I have the following definition
class A < ActiveRecord::Base
has_many bs, :order => ‘fecha ASC, id ASC’
end
somewhere in my app, I need to sort them differently:
some_a = A.find(something)
some_b = a.bs.find(:first,
:conditions => [‘fecha <= ?’, some_date],
:order => ‘fecha DESC, id DESC’)
this is creating the following SQL:
SELECT * FROM bs WHERE (bs.a_id = 1 AND (fecha <= ‘2007-02-01’)) ORDER
BY fecha DESC, id DESC, fecha ASC, id ASC LIMIT 1
wich is really weird… How can I replace the order of the relation only
for that query? Looking at the rails code[1], it seems that the only
(simple) way to do that would be to manually do the relation, something
like this:
some_b = B.find(:first,
:conditions => [‘a_id = ? AND fecha <= ?’,
some_a.id,
some_date],
:order => 'fecha DESC, id DESC)
[1] active_record(1.15.1)/associations/has_many.rb, line 81
if options[:order] && @reflection.options[:order]
options[:order] = “#{options[:order]},
#{@reflection.options[:order]}”
elsif @reflection.options[:order]
options[:order] = @reflection.options[:order]
end
line 82 seems to be where the :orders are mixed. Which is fine if
they’re not the same columns…
any idea?
regards,
Rolando.-