开发者

Question about update_all in Ruby

I created an array using this statement..

users_who_promoted = @organiza开发者_StackOverflow中文版tion.card_signups.select {|c| c.credit_status == true }

but when I do this :

users_who_promoted.update_all("credit_status = false")

I get a big error :

NoMethodError: undefined method `update_all' for #<Array:0x32377bc>
from (irb):25

Why is this?


You're calling update_all on an Array of ActiveRecord instances, when it is in fact a static method.

Your call should instead be User.update_all (or CardSignup, or whatever your class is called), then the update, then the conditions.

See: http://api.rubyonrails.org/classes/ActiveRecord/Base.html#M001787

EDIT for lack of clarity: having that array of values is only helpful insofar as it lets you construct a condition to pass in to update_all, of the form "id IN (#{users_who_promoted.map {|u| u.id}.join(",")})". If you don't think this will be a bottleneck in your application's performance, simply updating and saving each model object might be more readable.


Aha! This seems to work..

unless @organization.card_signups.empty?
  @organization.card_signups.update_all("credit_status = false")
end
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜