After reading about attr_accessible in the Rails 3.1 API, I see that there is an as :admin option in there. I would like to know two things.
-
If the user has an admin flag, how do does my controller tell my model that the user is an admin.
-
If the user is an owner, can i specify
:as => ownerin my model, and once again how does my controller inform my model they are the owner of an item.
There is no built-in integration with models; you pass in the role in the
assign_attributescall:The
:asparameter defaults to:default, and you can pass in any symbol that you want. To integrate this into yourUsermodel, you could give it an attribute calledrole, and then do something like:You can also bypass the protection using
:without_protection:In a similar way,
new,create,create!,update_attributes, andupdate_attributes!methods all respect mass-assignment security. The Ruby on Rails guide on security has more info.