My model includes an ‘admin’ field, which is true or false. It is not on ‘attr_accessible’ because we don’t want a bad guy to be able to trick our controller into giving ‘admin’ privs to a user.
Because, if I am logged in as an admin in my model, I ‘do’ want to be able to update any user’s record to make or take away admin from them. So… the lack of attr_accessible on admin prevents me from doing so.
Maybe that’s not the right way to look at it. What’s the right way to handle such a case?
Rails 3.2 added a feature that would allow you to do what you’re describing. Simply define this in your model:
And then in the controller where you want to assign the admin attribute, do this:
or
All the attributes that are listed prior to the
attr_accessiblecall with:as => :adminwill be accessible attributes for any Active Record call that supports the:as => :adminoption.If you didn’t call
accessible_attributesin that definition, then only the attributes defined there will be accessible.