views:

25

answers:

2

I created an array using this statement..

users_who_promoted = @organization.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?

A: 

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.

bnaul
+1  A: 

Aha! This seems to work..

unless @organization.card_signups.empty?
  @organization.card_signups.update_all("credit_status = false")
end
Trip
Interesting...that doesn't seem to work in my version of Rails (2.1.2), but I guess that's what I get for living in the stone age.
bnaul
This is 2.3.5 .. But its essentially what you suggested in using the method directly off the model which is in this case related to the organization model.
Trip