Hello all, I’m having a problem with activerecord and wondering if
anyone else has encountered this. (using rails 1.2.3)
I’m doing a find all and adding my own :order option, but whatever I
add, AR still adds another “created_at desc” to the end of the order
list. This is causing the query to blow up when created_at is ambiguous.
This is the query:
@user.notes.find(:all, :include =>
[:project],:order=>“notes.created_at”).each do |note|
and the sql ends up like this:
ActiveRecord::StatementInvalid (Mysql::Error: Column ‘created_at’ in
order clause is ambiguous: SELECT notes.id
AS t0_r0, notes.user_id
AS t0_r1, notes.project_id
AS t0_r2, notes.body
AS t0_r3,
notes.created_at
AS t0_r4, notes.updated_at
AS t0_r5, projects.id
AS t1_r0, projects.name
AS t1_r1, projects.position
AS t1_r2,
projects.user_id
AS t1_r3, projects.description
AS t1_r4,
projects.state
AS t1_r5, projects.created_at
AS t1_r6,
projects.updated_at
AS t1_r7, projects.default_context_id
AS t1_r8
FROM notes LEFT OUTER JOIN projects ON projects.id = notes.project_id
WHERE (notes.user_id = 1) ORDER BY notes.created_at, created_at DESC)
poking around Active record, I found this code from base.rb, which
appends an order by from the scope, but in the case above where is the
scope coming from?
def add_order!(sql, order, scope = :auto)
scope = scope(:find) if :auto == scope
scoped_order = scope[:order] if scope
if order
sql << " ORDER BY #{order}"
sql << ", #{scoped_order}" if scoped_order
else
sql << " ORDER BY #{scoped_order}" if scoped_order
end
end