Topic: paginate ordered by a column in an associated table

Hi all,

I have a Merchant model and a Product model.  I want to display all products in a paginated, sortable table.  That is pretty easy, the problem is that I'd like to include the associated merchant name for each product in the table AND I'd like the user to be able to sort this table by the merchant name.  I'm a n00b at ORM and I can't seem to see a way to order the paginate result by the merchant name.

Thanks for your help,

Scott

Re: paginate ordered by a column in an associated table

You need to include the merchants and the products in the same query results, probably using the :include parameter to include Merchants in your Products results.

Re: paginate ordered by a column in an associated table

I know you can do this in a normal find:

Product.find(:all, :include => 'merchant', :order => 'merchants.name')

You should be able to adapt this to the paginate method by simply using :include and :order there as shown here. Some people have had problems mixing pagination and :include. If this is the case you may need to separate the pagination method from the find method. I can go into more detail if you need me to.

Railscasts - Free Ruby on Rails Screencasts

Re: paginate ordered by a column in an associated table

works perfectly thanks! smile