Skip to content

Fix parent record should not get saved with duplicate children records#32952

Merged
kamipo merged 1 commit intorails:masterfrom
mechanicles:32940-fix
May 28, 2018
Merged

Fix parent record should not get saved with duplicate children records#32952
kamipo merged 1 commit intorails:masterfrom
mechanicles:32940-fix

Conversation

@mechanicles
Copy link
Copy Markdown
Contributor

@mechanicles mechanicles commented May 22, 2018

@rails-bot
Copy link
Copy Markdown

r? @kaspth

(@rails-bot has picked a reviewer for you, use r? to override)

@Mangara
Copy link
Copy Markdown
Contributor

Mangara commented May 22, 2018

Thanks for taking a look at this so quickly! Could you verify that the fix also works with these test cases?

many-to-many relation
many-to-many :through

@Mangara
Copy link
Copy Markdown
Contributor

Mangara commented May 22, 2018

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?

@mechanicles mechanicles force-pushed the 32940-fix branch 2 times, most recently from e1a3bf0 to 520298d Compare May 22, 2018 17:07
@mechanicles
Copy link
Copy Markdown
Contributor Author

Removed newly created models and used existing ones.

@mechanicles mechanicles force-pushed the 32940-fix branch 2 times, most recently from b66a935 to 252a600 Compare May 24, 2018 06:21
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This changes documented behaviour:

# [:validate]
# When set to +true+, validates new objects added to association when saving the parent object. +true+ by default.

Associations specified with validate: false should not prevent the parent record from being saved.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@eugeneius Thanks. I will go through it.

@mechanicles mechanicles force-pushed the 32940-fix branch 2 times, most recently from f9a6ab5 to 149bfde Compare May 28, 2018 08:01
@mechanicles
Copy link
Copy Markdown
Contributor Author

@eugeneius I have updated the code. Please check.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Calling association_valid? on the line above validates the record:

unless valid = record.valid?(context)

Calling insert_record also validates the record:

def insert_record(record, validate = true, raise = false, &block)
if raise
record.save!(validate: validate, &block)
else
record.save(validate: validate, &block)
end
end

We should pass false as the second argument to insert_record, to avoid the unnecessary second validation.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there any reason to switch from reflection.validate? to this?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was fine as an elsif, no need to change it.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There's no need to remove this line ✂️

Comment thread activerecord/CHANGELOG.md Outdated
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#32939 was already fixed by #32796.

@mechanicles mechanicles force-pushed the 32940-fix branch 2 times, most recently from 2042120 to 8660fda Compare May 28, 2018 12:50
@mechanicles
Copy link
Copy Markdown
Contributor Author

@eugeneius I have updated the code. Now I have passed the second argument false to insert_method. Please check and let me know if I missed something.

@kamipo kamipo merged commit d7a3f33 into rails:master May 28, 2018
kamipo added a commit that referenced this pull request May 28, 2018
Fix parent record should not get saved with duplicate children records
kamipo added a commit that referenced this pull request May 28, 2018
Fix parent record should not get saved with duplicate children records
@mechanicles mechanicles deleted the 32940-fix branch May 29, 2018 05:31
kamipo added a commit that referenced this pull request Jun 6, 2018
@jasonperrone
Copy link
Copy Markdown

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?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants