简体   繁体   中英

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:

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. ie &: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?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM