2014-03-28 30 views
0

我得到這個警告條件在軌貶值的使用範圍塊

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] 

有人可以幫助我?

+0

請參閱http://stackoverflow.com/questions/16569994/deprecation-warning-when-using-has-many-through-uniq-in-rails-4?rq=1 – Pavan

回答

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。參考Named scope better than conditions