rails中不赞成的条件使用作用域块



我收到了这个警告

DEPRECATION WARNING: The following options in your Product.has_many :recommended_users declaration are deprecated: :conditions. Please use a scope block instead. For example, the following:
has_many :spam_comments, conditions: { spam: true }, class_name: 'Comment'
should be rewritten as the following:
has_many :spam_comments, -> { where spam: true }, class_name: 'Comment'

我试着用新的作用域块重写我的代码,但没有成功:

has_many :current_products, :class_name => "Product", :through => :associations, :source => :product, :conditions => ["associations.category = ?", 1]
有人能帮我一下吗?

使用

has_many :current_products, -> { where "associations.category = ?", 1}, :class_name => "Product", :through => :associations, :source => :product

或者

has_many :current_products, -> { where "associations.category = 1"}, :class_name => "Product", :through => :associations, :source => :product

参考API dock查看语法和其他细节

也可以使用named_scope。引用命名作用域优于

条件

最新更新