-1

パラメータを持つルートがあります: localhost:3000/points/mypoint.json?access_token=acuotodEhqyFnVQPJ2AK このルートは、ユーザーのすべてのレストランのリスト ポイントを返します (access_token からユーザーを取得します) 私のコード:

def mypoint
      @locations = Location.select("locations.*")
end
def totalpoint
    @totalpoint = 0
    return 0 if self.user_points.point.count==0
    self.user_points.point.collect { |p| @totalpoint = @totalpoint +p.points}
    return @totalpoint
end
scope :point, where( "points IS NOT ?", nil )

Rabl ファイル:

collection @locations
attributes :name, :totalpoint

データベース:

--id---user_id---point_type--restaurant_id---points---
  1       1       favourite       1            50
  2       7       ratings         1            123
  3       7       comments        1            50
  4       7       comments        2            50
  5       7       ratings         2            10

結果:

[
    {
        "name": "quoc",
        "totalpoint": 223
    },
    {
        "name": "Restaurant_id2",
        "totalpoint": 60
    }
]

ただし、これはすべてのレストランの結果リストであり、ユーザーに関するものではありません。totalpoint メソッドに条件 user_id を追加するように教えてください。user_id = 1 の Result の下に結果を表示したい:

[
    {
        "name": "quoc",
        "totalpoint": 50
    },
    {
        "name": "Restaurant_id2",
        "totalpoint": 0
    }
]

user_id = 7 の結果:

[
    {
        "name": "quoc",
        "totalpoint": 173
    },
    {
        "name": "Restaurant_id2",
        "totalpoint": 60
    }
]

解決策を教えてください。ありがとうございます

4

1 に答える 1

0

質問の詳細を適切に説明してください。理解するのに 15 分かかりました。

モデル自体の中にメソッドを書くことができます。モデル名は何ですか? user_id でポイントのリストを取得するには

最初に一意の user_id と restaurant_id をすべて見つけます

      a=Model.select("DISTINCT(restaurant_id)")
      b=Model.select("DISTINCT(user_id)")

この例では、a=[1,2] および b=[1,7] で、各レストランでユーザーが収集したすべてのポイントを取得します。

     score=Array.new

     b.each do |k|
       rest=Hash.new
       a.each do |f|
          rest[f]=Model.find(:all,:conditions=>["user_id=? and restaurant_id",k,f],:select=>["points"] )
       end
     score<<rest
      end

これでできます。

于 2013-11-06T08:32:04.533 に答える