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).
<%= sort_link('Date (just the date)', 'news_items.date') %> |
<%= sort_link('Stock Name (stock.name)', 'stocks.name') %> |
<%= sort_link('Sector Name', 'stocks.sector.name') %> |
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…
<%= sort_link('Date (just the date)', 'news_items.date') %> |
<%= sort_link('Stock Name (stock.name)', 'stocks.name') %> |
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 !