Order_by with property

I have an usual pagination in rails. But I am trying to sort by a
property which is not in the same table on the database. (I have an
Entry class which has a region_id. Then I got a Region class with id and
name. Now I would like to sort the Entries in my Pagination with the
name of the region.)

If I use a collection I have to load the whole bunch and therefore this
is not an option. Do I need to do a find_by_sql? Or do I miss anything?

thanks
h.

huli wrote:

I have an usual pagination in rails. But I am trying to sort by a
property which is not in the same table on the database. (I have an
Entry class which has a region_id. Then I got a Region class with id and
name. Now I would like to sort the Entries in my Pagination with the
name of the region.)

If I use a collection I have to load the whole bunch and therefore this
is not an option. Do I need to do a find_by_sql? Or do I miss anything?

You can pass all the options you would usually pass to find to paginate,
so in this case you could use :joins => “INNER JOIN regions on
regions.id = region_id” and then you can :order => ‘regions.name’

Fred

Fred wrote:

huli wrote:

I have an usual pagination in rails. But I am trying to sort by a
property which is not in the same table on the database. (I have an
Entry class which has a region_id. Then I got a Region class with id and
name. Now I would like to sort the Entries in my Pagination with the
name of the region.)

If I use a collection I have to load the whole bunch and therefore this
is not an option. Do I need to do a find_by_sql? Or do I miss anything?

You can pass all the options you would usually pass to find to paginate,
so in this case you could use :joins => “INNER JOIN regions on
regions.id = region_id” and then you can :order => ‘regions.name’

Fred

Cool thats it! Thanks a lot!

h.

This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.

| Privacy Policy | Terms of Service | Remote Ruby Jobs