Joh*_*hir 5 ruby ruby-on-rails associations
我有这个协会:
has_many :foo_participators, through: :foos, source: :user, conditions: "foos.state = 'completed'"
Run Code Online (Sandbox Code Playgroud)
Rails告诉我:
弃用警告:不推荐使用Bar.has_many:foo_participators声明中的以下选项:: condition.请改用示波器块.例如,以下内容:
Run Code Online (Sandbox Code Playgroud)has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment'应该改写如下:
Run Code Online (Sandbox Code Playgroud)has_many :spam_comments, -> { where spam: true }, class_name: 'Comment'
这与我的协会有可能吗?
在我提出这个问题后立即想出来.出于某种原因,我没有尝试过将lambda放在第一位 - 这样做非常有效.
has_many :foo_participators, ->{where "foos.state = 'completed'"}, through: :foos, source: :user
Run Code Online (Sandbox Code Playgroud)