custom assignable primary key and mass assignment filter/warning #4291

Closed
sanderhahn opened this Issue Jan 4, 2012 · 4 comments

3 participants

@sanderhahn

The MassAssignmentSecurity triggers a warning and seems to clear the primary key when you use a custom set_primary_key and disable the id auto primary key.

Loading development environment (Rails 3.1.3)

ruby-1.9.2-p290 :001 > CurrencyUnit.new({code: 'USD', name: 'Dollar'})
WARNING: Can't mass-assign protected attributes: code
 => #[CurrencyUnit code: nil, name: "Dollar", created_at: nil, updated_at: nil]

ruby-1.9.2-p290 :002 > CurrencyUnit.primary_key
 => "code" 

You don't receive any warnings when models have a default id key.

@tenderlove
Ruby on Rails member

Do you mind creating a test case for this?

@dpickett

Well primary keys are protected by default per: https://github.com/rails/rails/blob/master/activerecord/lib/active_record/attribute_assignment.rb#L13-15

So the errant behavior here is that a warning is emitted for one case but not the other?

@dpickett

I can confirm that a warning is emitted with both nondefault primary keys and the default id key

In a freshly generated rails app running 3.1.3

ruby-1.9.2-p180-patched :001 > Post.new(id: 5)
WARNING: Can't mass-assign protected attributes: id
 => #<Post id: nil, name: nil, created_at: nil, updated_at: nil> 
ruby-1.9.2-p180-patched :002 > Rails.version
 => "3.1.3" 

This issue should be closed.

@sanderhahn

Thanks, i misunderstood that field became protected because it became the primary key. Sorry, issue closed.

@sanderhahn sanderhahn closed this Jan 15, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment