2013-11-27 21 views
4

更換條件我有這樣的聯想:上的has_many:通過協會,與拉姆達

has_many :foo_participators, through: :foos, source: :user, conditions: "foos.state = 'completed'" 

Rails的告訴我:

棄用警告:在你的Bar.has_many 以下選項:foo_participators聲明已棄用::條件。請改用示波器 。例如,以下內容:

has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment' 

應該被改寫爲以下幾點:

has_many :spam_comments, -> { where spam: true }, class_name: 'Comment' 

這可能與我的聯想?

回答

4

我問了這個問題後就想到了這一點。出於某種原因,我沒有嘗試將lambda放在第一位 - 這樣做完美無缺。

has_many :foo_participators, ->{where "foos.state = 'completed'"}, through: :foos, source: :user