Ah, so you've got a grandchild object (sector).  I just had a parent & one 
child.  I bet you are close.  Try this:

  @news_items = NewsItem.find(:all, :order => order_by, :include => ['stock', 
'sector'])

and in the view:

  <th><%= sort_link('Sector Name', 'sectors.name') %></th>

(So--singular forms in :include--you're naming associated classes, but plural 
form in the string that gets passed to :order, as that just gets 
unceremoniously squirted into the SQL statement, so it's got to be a valid 
table_name.field_name designation.)

I *think* that will work.

HTH,

-Roy

-----Original Message-----
From: [email protected] [mailto:[EMAIL PROTECTED] On Behalf Of 
bingo bob
Sent: Wednesday, October 22, 2008 10:55 PM
To: [email protected]
Subject: [Rails] Re: sorting in different ways (with the same index action)



Excellent!

That works! well at least partly, I'm wondering if my relationships are
set up correctly...

here's some code that I am using

(top two work, bottom one not).

<th><%= sort_link('Date (just the date)', 'news_items.date') %></th>
<th><%= sort_link('Stock Name (stock.name)', 'stocks.name') %></th>
<th><%= sort_link('Sector Name', 'stocks.sector.name') %></th>

this is the error... "SQLite3::SQLException: no such column:
stocks.sector.name: SELECT "news_items"."id" AS t0_r0,
"news_items"."news_type_id" AS t0_r1, "news_items"."stock_id" AS t0_r2,
"news_items"."description" AS t0_r3, "news_items"."date" AS t0_r4,
"news_items"."created_at" AS t0_r5, "news_items"."updated_at" AS t0_r6,
"stocks"."id" AS t1_r0, "stocks"."name" AS t1_r1, "stocks"."ticker" AS
t1_r2, "stocks"."active" AS t1_r3, "stocks"."sector_id" AS t1_r4,
"stocks"."created_at" AS t1_r5, "stocks"."updated_at" AS t1_r6 FROM
"news_items"  LEFT OUTER JOIN "stocks" ON "stocks".id =
"news_items".stock_id      ORDER BY stocks.sector.name"  <- clearly
doesn't like stocks.sector.name

here's teh cont code

    order_by = params[:order_by] || 'news_items.date'
    @news_items = NewsItem.find(:all, :order => order_by, :include =>
'stock')

---

I also tried

    @news_items = NewsItem.find(:all, :order => order_by, :include =>
'stock', 'sector')

and

    @news_items = NewsItem.find(:all, :order => order_by, :include =>
'stock', 'sectors')

I feel it's close...


As i say, these work perfectly..

<th><%= sort_link('Date (just the date)', 'news_items.date') %></th>
<th><%= sort_link('Stock Name (stock.name)', 'stocks.name') %></th>

models are a bit like this...

class Stock < ActiveRecord::Base
  belongs_to :sector
  has_many :news_items
end

class Sector < ActiveRecord::Base
  has_many :stocks
end

class NewsType < ActiveRecord::Base
end

class NewsItem < ActiveRecord::Base
  belongs_to :stock
  belongs_to :news_type
  belongs_to :sector
end


I'd like to sort by all of those....

phew. not easy this relational db stuff !

--
Posted via http://www.ruby-forum.com/.



--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby 
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to