Fix parent record should not get saved with duplicate children records#32952
Fix parent record should not get saved with duplicate children records#32952kamipo merged 1 commit intorails:masterfrom
Conversation
|
r? @kaspth (@rails-bot has picked a reviewer for you, use r? to override) |
|
Thanks for taking a look at this so quickly! Could you verify that the fix also works with these test cases? |
|
I'm not sure if it's worth creating new models for the test cases. Would something similar to the tests in PR #11851 work here? |
e1a3bf0 to
520298d
Compare
|
Removed newly created models and used existing ones. |
b66a935 to
252a600
Compare
There was a problem hiding this comment.
This changes documented behaviour:
rails/activerecord/lib/active_record/associations.rb
Lines 1338 to 1339 in 6c574ac
Associations specified with validate: false should not prevent the parent record from being saved.
There was a problem hiding this comment.
@eugeneius Thanks. I will go through it.
f9a6ab5 to
149bfde
Compare
|
@eugeneius I have updated the code. Please check. |
There was a problem hiding this comment.
Calling association_valid? on the line above validates the record:
Calling insert_record also validates the record:
rails/activerecord/lib/active_record/associations/collection_association.rb
Lines 376 to 382 in 36390b6
We should pass false as the second argument to insert_record, to avoid the unnecessary second validation.
There was a problem hiding this comment.
Is there any reason to switch from reflection.validate? to this?
There was a problem hiding this comment.
This was fine as an elsif, no need to change it.
There was a problem hiding this comment.
There's no need to remove this line ✂️
2042120 to
8660fda
Compare
|
@eugeneius I have updated the code. Now I have passed the second argument |
Fix parent record should not get saved with duplicate children records
Fix parent record should not get saved with duplicate children records
|
I have a question about this code. Am I seeing it right that insert_record is called with validate=false if reflection.validate? is true, and called with validate=true if reflection.validate? is false? |
Uh oh!
There was an error while loading. Please reload this page.