This project is archived and is in readonly mode.
Regression: mass assignment interaction with a non-standard primary key (2.3.5 -> 2.3.10)
-
Cesario
I'm afraid this is not a bug, and it can be reproduced on 2.3.5, 2.3.10 or 3.0.3.
Primary keys are marked as protected, this test case will not fail:class Subscriber < AR::Base set_primary_key :nick attr_accessible :nick # Mark it available for mass-assignment end def test_customized_string_primary_key_set_with_create subscriber = Subscriber.create :nick => 'webster123' assert_equal 'webster123', subscriber.id assert_equal 'webster123', subscriber.nick endMass-assigning the key is just prevented unless you set it explicitly accessible.
Even on 2.3.5, if I remove the
attr_accessibleline, i'm getting this result for the same test:1) Error: test_customized_string_primary_key_set_with_create(PrimaryKeysTest): ActiveRecord::StatementInvalid: SQLite3::ConstraintException: subscribers.nick may not be NULL: INSERT INTO "subscribers" ("name") VALUES(NULL) -
mat
I get that primary keys are protected, I don't get how the migration ever worked a year ago. Something's changed somewhere.
I'll add the attr_accessible and we can all move on!
Thanks
M.
-
Cesario
I won't jump into the code right now, but you could check in 2.3.5, at runtime, if the
isocolumn has been set automaticallyattr_protectedwhen you ranset_primary_key.In any case, I think we could close this ticket. I hope it helped :)
-
mat
Yes, let's close it. Your explanation about why primary_keys are by default protected was very helpful and makes sense.
I wish there was somewhere we could document that you need to open up the primary key for mass assignment if the purpose is to set it yourself. All the examples I found were to use things like sequences in Postgres, etc.
Thanks
-
Rohit Arondekar
- State changed from new to invalid
- Importance changed from to Low