开发者

conditional update_all with join tables in ActiveRecord?

开发者 https://www.devze.com 2023-02-17 11:41 出处:网络
The following query returns the collection of AR objects that I want to update: Variant.all(:joins => { :candy_product => :candy }, :conditions => \"candies.name = \'Skittles\'\")

The following query returns the collection of AR objects that I want to update:

Variant.all(:joins => { :candy_product => :candy }, :conditions => "candies.name = 'Skittles'")

I'm trying to do something lik开发者_如何学运维e the following:

Variant.update_all(:price => 5, :joins => { :candy_product => :candy }, :conditions => "candies.name = 'Skittles'")

This should only update the price for the variants returned from original query. Is this possible with AR or will I have to write the SQL? This is a pretty large collection, so anything that iterates is out.

Using Rails 2.3.4.


As @François Beausoleil pointed correctly we should use scoped

Variant.scoped(:joins => { :candy_product => :candy }, :conditions => "candies.name = 'Skittles'").update_all(:price => 5)
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号