I have a question about using attr_accessible in Rails.
I sometimes want to set guard_protected_attributes to false in order to bypass mass assignment protection. I’m wondering why the following line doesn’t work (it creates the “can’t stringify keys” error):
@user.attributes=({ :name => "James Bond", :admin => true }, false)
…but this does:
@user.send(:attributes=, { :name => "James Bond", :admin => true }, false)
Anyone know the reason?
Because the Ruby parser parses ‘
{ :name => "James Bond", :admin => true}, false‘ as the single argument to#attributes=. Calling a method ‘foo=‘ limits you to one argument in Ruby. Thesendgets around that.What’s actually happening is that Rails is trying to stringify the keys of
false, which, being aFalseClassrather than aHash, doesn’t have any.