パラメータを持つルートがあります: 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
}
]
解決策を教えてください。ありがとうございます