Sorting attribute in list from foreign table

Hello, I currently have two tables.

table_a (
id
table_b_id
other_attrs
)

table b (
id
name
)

So table A holds an id from table B. During the listing of tuples in
table A, it lists table A’s attributes. In place of the table B id, I
check the id, and instead display the name corresponding to that id. Now
I want to be able to sort the listing of tuples in table A by the names
of table B. Here is some code to hopefully help you understand more
clearly what I’m trying to do.

sort = case @params[:sort]
       when "last inserted"  then "id DESC"
       when "name"  then "table_b.name" # <---- does not work
       end

@table_a_pages, @table_as = paginate :table_a, :order => sort, 

:per_page => 10

At the noted line, I am unable to use table_b.name to sort the list. Any
suggestions on how to solve this? Thanks!

-Gilles

you’ll need to include a reference to table b,

paginate builds a find statement, and you weren’t telling it to
include table_b

@table_a_pages, @table_as = paginate :table_a, :order =>  

“table_b.name”,
:per_page => 10, :include => [:table_b]

J

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