2015-07-12 33 views
0

我想通過該模型中的方法對結果進行排序,但模型方法需要一些參數,這就是我遇到問題的地方。 我的模式被稱爲事件,我試圖用Ruby on Rails 4 - 使用模型方法用參數排序

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

我敢肯定,問題是使用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?