Ruby on Rails 4 - 使用模型方法对params进行排序

时间:2015-07-12 05:25:36

标签: ruby-on-rails

我试图通过该模型中的方法对结果进行排序,但模型方法需要一些参数,这就是我遇到问题的地方。 我的模型叫做Event,我试图使用

Event.all.sort_by(&:user_score("2", "51.4980749", "10.8119977"))

方法用户得分需要一些参数

def user_score user_id, latitude, longitude  
  return 0 
end

返回0当然只是为了测试它,但是当我调用函数时它已经失败了:

SyntaxError: (irb):12: syntax error, unexpected '(', expecting ')'
...Event.all.sort_by(&:user_score("2", "51.4980749", ...

我做错了什么?

1 个答案:

答案 0 :(得分:1)

我确定,问题在于使用block表示法调用方法。即&:user_score("2", "51.4980749", "10.8119977")

请您以更明确的方式尝试如下,

Event.all.sort_by{|e| e.user_score("2", "51.4980749", "10.8119977")}

OR

请找到这篇文章Can you supply arguments to the map(&:method) syntax in Ruby?