elasticsearch/elasticsearch-rails — pull request #36 by fcheung (2014-03-10 20:21:43+00:00)
Title: Elasticsearch::Model::Adapter::ActiveRecord not ensuring order in some cases

The adapter adds an order singleton method that in effect removes the special version of to_a added earlier.
However, calling first and last adds an explicit order by primary key, so when calling these methods we always throw away the elastic search generated ordering

I've attached a failing test for this.

The where('1=1') is importing here (anything like joins, includes, etc.) would do: without it the implementation of first that is called is the one from Enumerable, rather than calling first on the relation

I also noticed that some of the [finder methods](https://github.com/rails/rails/blob/master/activerecord/lib/active_record/relation/finder_methods.rb#L460) operate on `@records` directly if it is loaded - there could be some edge cases here.

Not sure what the best way forward is. One way would be to make this a true relation by doing something like `.order("fields(#{klass.primary_key},1,2,3")` so that then the relation is just a normal relation. Or perhaps what `records` requires is just a bit too clever and makes too much use of AR internals