A pretty common coding pattern in Ruby on Rails is the following:
app/controller/users_controller.rb: def update @user = User.find(params[:id]) params[:user].delete(:admin) # make sure to protect admin flag respond_to do |format| if @user.update_attributes(params[:user]) [...]One would assume here that params[:user].delete(:admin) would not allow the admin flag to be assigned.
Observe the following Ruyb on Rails code:
rails/activerecord/lib/active_record/attribute_assignment.rb: 01 def assign_attributes(new_attributes, options = {}) 02 return unless new_attributes 03 04 attributes = new_attributes.stringify_keys 05 multi_parameter_attributes = [] 06 nested_parameter_attributes = [] 07 @mass_assignment_options = options 08 09 unless options[:without_protection] 10 attributes = sanitize_for_mass_assignment(attributes, mass_assignment_role) 11 end 12 13 attributes.each do |k, v| 14 if k.include?("(") 15 multi_parameter_attributes << [ k, v ] 16 elsif respond_to?("#{k}=") 17 if v.is_a?(Hash) 18 nested_parameter_attributes << [ k, v ] 19 else 20 send("#{k}=", v) 21 end 22 else 23 raise(UnknownAttributeError, "unknown attribute: #{k}") 24 end 25 end 26 27 # assign any deferred nested attributes after the base attributes have been set 28 nested_parameter_attributes.each do |k,v| 29 send("#{k}=", v) 30 end 31 32 @mass_assignment_options = nil 33 assign_multiparameter_attributes(multi_parameter_attributes) 34 endSo if the model does not use the build in mass assignment protection (attr_protected/attr_accessible),
def attributes=(new_attributes) return unless new_attributes.is_a?(Hash) assign_attributes(new_attributes) end
user[name(1)]=first_name&user[name(2)]=last_name
user[admin(1)]=true