The official way of preventing security risks with mass-assignment is using attr_accessible. However, some programmers feel this is not a job for the model (or at least not only for the model). The simplest way of doing it in a controller is slicing the params hash:
@user = User.update_attributes(params[:user].slice(:name))
However the documentation states:
Note that using Hash#except or Hash#slice in place of attr_accessible to sanitize attributes won’t provide sufficient protection.
Why is that? Why a whitelist-slicing of params does not provide enough protection?
UPDATE: Rails 4.0 will ship strong-parameters, a refined slicing of parameters, so I guess the whole slicing thing was not so bad after all.
attr_accesible
you can use:name
in your model if you need to (albeit it without saving it), but if you.slice
it off theparams
hash you can't do that. It's also much more semantic to useattr_accesible
because it tells others the properties relationship with the model, whereas slicing it is much more cryptic. – Voidance