Brandon M Thu Aug 25 23:35:29 -0400 2011

Subject: Order using columns on another table

Hey guys,

I'm trying to do an order like this:
page::find('all', array('conditions' => 'book_id = 5', 'order' => 'book.updated_at'));

I know that's not right, but hopefully explains what I'm looking for. The column I want to use on the order isn't on the table that I'm currently working with but pulled in using $belongs_to in the class.

Any way to do this without writing a custom sql statement using find_by_sql?


Clay vanSchalkwijk Wed Sep 07 16:59:20 -0400 2011

You would need to join on that table first and specify the direction you are sorting:

1 Page::find('all', array('conditions' => 'page.book_id = 5',  'joins' => 'LEFT JOIN book b ON b.book_id = page.book_id', order' => 'b.updated_at DESC'  ));

(1-1/1)