Skip to content

[MODEL] Avoid making an update when no attributes are changed #762

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Aug 7, 2018

Conversation

nilbus
Copy link
Contributor

@nilbus nilbus commented Dec 15, 2017

@dim0627 pointed out in #743 that calling update_attributes({}) on an ActiveRecord model instance triggers the before_save callback, even when there are no attributes to update. This results in an unnecessary empty update request to elasticsearch.

Using ActiveSupport's presence, it will act the same for both nil and {}, which are both ways Rails can express that there are no changes to save.

Closes #743

@dim0627
Copy link

dim0627 commented Dec 15, 2017

Thank you for making PR for my problem though 🙏

I think this changes should use presence instead of .present?.
Or if (attributes_in_database = self.instance_variable_get(:@__changed_model_attributes)).present?.

Otherwise, boolean will be assigned to attributes_in_database.

@nilbus
Copy link
Contributor Author

nilbus commented Dec 15, 2017

Fantastic catch. I had interpreted it as a boolean, but the tests were just telling me the same thing. 😄

@nilbus
Copy link
Contributor Author

nilbus commented Dec 15, 2017

Tests updated and passing (locally).

@nilbus
Copy link
Contributor Author

nilbus commented Dec 15, 2017

@dim0627 I should point out that this change only replaces the update call with an index call.

else
index_document(options)
end

This change makes the behavior more consistent, but is it actually going to be helpful to you?

@dim0627
Copy link

dim0627 commented Dec 15, 2017

@nilbus
Yes, I confused Why don't called index_document!?.
Your change is looks good to me 👍
Thanks!

@estolfo
Copy link
Contributor

estolfo commented Aug 7, 2018

@nilbus and @dim0627 thanks for your work on this! I've tested the changes locally and will merge.

@estolfo estolfo merged commit 99b0f39 into elastic:master Aug 7, 2018
@nilbus
Copy link
Contributor Author

nilbus commented Aug 7, 2018

Thanks! 🎉

@nilbus nilbus deleted the empty-update branch November 1, 2018 03:53
@nilbus nilbus restored the empty-update branch November 1, 2018 03:53
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.

None yet

3 participants