[英]Ruby on Rails 4 - Use model method to sort with params
I'm trying to sort my results by a method in that model but the model method needs some arguments, that's where I have problems. 我正在尝试按该模型中的方法对结果进行排序,但是模型方法需要一些参数,这就是我遇到的问题。 My model is called Event and I'm trying to use
我的模型称为事件,我正在尝试使用
Event.all.sort_by(&:user_score("2", "51.4980749", "10.8119977"))
The method user score needs some arguments 方法用户分数需要一些参数
def user_score user_id, latitude, longitude
return 0
end
The return 0 of course is just to test it but it already fails when I call the function: 返回值0当然只是为了测试它,但是当我调用该函数时它已经失败了:
SyntaxError: (irb):12: syntax error, unexpected '(', expecting ')'
...Event.all.sort_by(&:user_score("2", "51.4980749", ...
What am I doing wrong? 我究竟做错了什么?
I'm sure, the problem is with calling method using block
notation. 我敢肯定,问题在于使用
block
符号的调用方法。 ie &:user_score("2", "51.4980749", "10.8119977")
即
&:user_score("2", "51.4980749", "10.8119977")
Can you please try the same in more explicit way as follows, 您能否按以下更明确的方式尝试相同的操作,
Event.all.sort_by{|e| e.user_score("2", "51.4980749", "10.8119977")}
OR 要么
Please find this post Can you supply arguments to the map(&:method) syntax in Ruby? 请找到这篇文章。您能为Ruby中的map(&:method)语法提供参数吗?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.