Hi,
I was just working with a model where I am using attr_protected as
only certain attributes should be set depending on the context and I
was wondering if there is a convention when it comes to setting
multiple protected attributes?
e.g.
# The rubbish way
user = User.find(id)
user.name = params[:user][:name]
user.email = params[:user][:email]
user.save
One attribute per line is a bit tiresome, so you can do:
[:name, :email].each do |attr|
user.send("#{attr}=", params[:user][attr])
end
Is there an obvious nicer way I'm missing?
As an alternative I have made a quick monkey patch to AR::Base that
allows:
user.update_attributes_safely([:name, :email], params[:user])
This just picks the specified keys from the hash, ignoring the
protected attributes, but to me seems just as safe? Update_attributes
itself could easily be modified too instead of adding another method.
Anyone else do similar?
Regards,
Andrew
--
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.