Andrew France wrote:
> 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?

class User << ActiveRecord::Base
  attr_accessible :name, :email
  ...
  ...
end

Only :name and :email is allowed to be set through mass assignment.
-- 
Posted via http://www.ruby-forum.com/.
-- 
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.


Reply via email to