I have a query of the form

  result=Parent.includes(:children).references(:children).where('....')

and I would like to have the result sorted in a particular way. The
comparision function for the sort is complex and can't be expressed by
simply ordering ascendingly or descendingly on the fields, but  depends
on a complex term based on fields from both :parents and :children.

I currently use the following approach:

  ordered_result=result.to_a.sort { |a,b| my_sort_function(a,b) }

I am aware that this means that all the records  have to be present in
memory. If there is an easy way to avoid this, it would be nice, but
since  I can ensure (from the context of the application), that the
number of records returned from the query is always smaller than a
certain, system-wide constant (currently 250), this is not a real
problem.

However, I wonder whether my approach to convert the
ActiveRecord::Relation to an array is a good idea. Maybe there is a
better way to implement sorting?

Ronald

-- 
Posted via http://www.ruby-forum.com/.

-- 
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit 
https://groups.google.com/d/msgid/rubyonrails-talk/6a3b981e64a12f0efa8e25cfe5dc4c97%40ruby-forum.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to