Pete Yandell
2007-Sep-28 04:26 UTC
Controlling attribute mass assignment in the controller
Rails nicely lets me do this in a controller: @user = User.new(params[:user]) and provides the attr_protected (and attr_accessible) model methods to hide attributes from this sort of the mass assignment. Problem is, I quite often want to control which attributes are assigned on a per-action basis, which leaves me doing things like: @user = User.new @user.name = params[:user][:name] @user.address = params[:user][:address] Not very DRY. The alternative that I''ve been using recently is this: @user = User.new(params[:user].only(:name, :address)) where the "only" method is defined as: class Hash def only(*keys) keys.map!(&:to_s) Hash[*self.select {|key, value| keys.include?(key) }.flatten] end end Do you think this is a nice way to do it? Got suggestions for alternatives or improvements? Regards, Pete Yandell http://notahat.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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Jean-François Trân
2007-Sep-28 10:11 UTC
Re: Controlling attribute mass assignment in the controller
Pete :> Not very DRY. The alternative that I''ve been using recently is this: > > @user = User.new(params[:user].only(:name, :address)) > > where the "only" method is defined as: > > class Hash > def only(*keys) > keys.map!(&:to_s) > Hash[*self.select {|key, value| keys.include?(key) }.flatten] > end > end > > Do you think this is a nice way to do it? Got suggestions for > alternatives or improvements?Have a look at ActiveSupport''s Hash#slice in Edge : http://dev.rubyonrails.org/browser/trunk/activesupport/lib/active_support/core_ext/hash/slice.rb Hash#assert_valid_keys as a guard can also be useful. -- Jean-François. -- Ruby ( http://www.rubyfrance.org ) on Rails ( http://www.railsfrance.org ) --~--~---------~--~----~------------~-------~--~----~ 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 rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---