Skip to content
New issue

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

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

Already on GitHub? # to your account

[MODEL] Ensure that specified ActiveRecord order is not overwritten by Elasticsearch search results order #835

Merged
merged 2 commits into from
Sep 17, 2018

Conversation

estolfo
Copy link
Contributor

@estolfo estolfo commented Sep 13, 2018

This pull request resolves the following issues

And is related to this pull request, which partially resolved the issue.
#831

@estolfo estolfo changed the title [MODEL] Ensure that specified ActiveRecord order is not overwritten by Elasticsearch query order [MODEL] Ensure that specified ActiveRecord order is not overwritten by Elasticsearch search results order Sep 13, 2018
Copy link
Contributor

@jazzytomato jazzytomato left a comment

Choose a reason for hiding this comment

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

Looks good! I think we could even remove the code which redefines the to_a method to the original one?

def order(*args)
sql_records = records.__send__ :order, *args
# Redefine the `to_a` method to the original one
#
sql_records.instance_exec do
ar_records_method_name = :to_a
ar_records_method_name = :records if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 5
define_singleton_method(ar_records_method_name) do
if defined?(::ActiveRecord) && ::ActiveRecord::VERSION::MAJOR >= 4
self.load
else
self.__send__(:exec_queries)
end
@records
end
end
sql_records
end

@estolfo
Copy link
Contributor Author

estolfo commented Sep 14, 2018

@jazzytomato Good catch, I'll test it and see if we can remove that. Thanks for your review!

@estolfo
Copy link
Contributor Author

estolfo commented Sep 14, 2018

@jazzytomato I've pushed another commit that removes the interception of #order, as it's not necessary with the check on order_values in the #records method. What do you think?

@estolfo estolfo mentioned this pull request Sep 14, 2018
@estolfo estolfo merged commit 2a5f57b into elastic:6.x Sep 17, 2018
estolfo added a commit that referenced this pull request Sep 17, 2018
…y Elasticsearch search results order (#835)

* [MODEL] Ensure that specified ActiveRecord order is not overwritten by Elasticsearch query order

* [MODEL] Remove interception of #order method, as ordering is handled by checking order_values in #to_a
# for free to join this conversation on GitHub. Already have an account? # to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants