[Rails] sorting attribute in list from foreign table

Jodi Showers
Tue, 15 Aug 2006 22:16:02 -0000

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

On 15-Aug-06, at 5:19 PM, Gilles wrote:

> 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
>
> --
> Posted via http://www.ruby-forum.com/.
> _______________________________________________
> Rails mailing list
> Rails@lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails