views:

41

answers:

1

I'm making a user administration page. For the system I'm creating, users need to be approved. Sometimes, there will be many users to approve, so I'd like to make that easy. I'm storing this as a boolean column called approved.

I remembered the Edit Multiple Individually Railscast and thought it would be a great fit. However, I'm running into problems which I traced back to ActiveRecord::Base#update.

update works fine in this example:

>> User.all.map(&:username)
=> ["ben", "fred"]
>> h = {"1"=>{'username'=>'benjamin'}, "2"=>{"username"=>'frederick'}}
=> {"1"=>{"username"=>"benjamin"}, "2"=>{"username"=>"frederick"}}
>> User.update(h.keys, h.values)
=> ...
>> User.all.map(&:username)
=> ["benjamin", "frederick"]

But not this one:

>> User.all.map(&:approved)
=> [true, nil]
>> h = {"1"=>{'approved'=>'1'}, "2"=>{'approved'=>'1'}}
>> User.update(h.keys, h.values)
=> ...
>> User.all.map(&:approved)
=> [true, nil]

Chaging from '1' to true didn't make a difference when I tested.

What am I doing wrong?

+2  A: 

may be you are using in your user.rb

attr_protected :approved
Salil
Close -- it was `attr_accessible`! Weird thing was, I could set it on the models directly by doing `user.approved = true`. That shouldn't have been the case, right? (I'm using `authlogic` -- maybe `acts_as_authentic` is playing a role?)
Benjamin Oakes
Oh... n/m. Hadn't read the docs in a while: "[attr_accessible] specifies a white list of model attributes that can be set via mass-assignment"
Benjamin Oakes