I've got a load of news_items that I find and sort by date in my
controller and then display in the view. Works just fine. I do this in
the index action.
It appears as tabular information, with column headings. As I say by
default I'm sorting by date.
What I'd like to do is after the list is rendered in the view, I'd like
to offer the user the ability to click on a different column heading to
sort by that column.
e.g. allow the user to sort by news_item.stock.sector
What I think I need is a link_to helper at the top for each column
heading to pass something back to the index action of the controller so
that I can repopulate the @news_items with a newly sorted list (as the
user clicks different column headings).
IMO you are on the right way. Pass a sort_by parameter to your index
action and use it as :order option for your News.find method..
OK that's great, could I trouble you for a short example?
def index
if (the link_to has returned some :order)
find some stuff in some order
else if (the link_to has returned some :order)
find some stuff in some order
end
end
def sort_link(show_text, sort_by)
link_to show_text, {:action => 'index', :order_by => sort_by}, :class => 'big-link', :title => "Sort by #{show_text}"
end
One thing I haven't done & would like to is to have the app 'remember' what the previous sort order was, so that users can toggle the ascending/descending nature of the sort.
Thanks, this is great. It works and I can see the value of the helper!
Makes the controller code so much easier. Seem "right".
I could do with a bit more help though.
1) Out of interest - how do you use the [:title => "Sort by
#{show_text}"] bit?
2) Most importantly, I got this working when I sort by columns in my
news_items table, e.g. news_item.date BUT I can't get it to work for me
as I ahve relationships built up such that I have, news_item.stock and a
stock has a name. How would I use this to sort my news_items by the
names of fields of items with them, e.g. a news_item stock name, or a
news_item news_type name.
Yeah, that's a problem I haven't actually tackled myself yet. If you notice, I did this:
<th><%= sort_link('Status', 'status_id') %></th>
Which does the sort by the numeric value of the id for the status, rather than the text the user sees. Very bush-league.
So I fixed it just now like so:
- Add an :include clause in the .find call to bring the fields from the child table into the SQL generated by AR.
- Changed my field references to include the table names, for any fields whose names are in both tables.
# controller
def index
order_by = params[:order_by] || 'projects.name' #<-- added the table prefix, since both Projects and Statuses have name fields.
@projects = Project.find(:all, :order => order_by, :include => 'status') #<-- The :include causes rails to join both tables in the resulting query.
This seems to work pretty well. I'm not sure how well it would scale if you've got e.g., fifteen different child objects you need to bring in.
The {:title => } stuff causes the <a> tags to have a title attribute, which gets shown in a tooltip when the user hovers over the link. (link_to takes an optional hash of tag attributes.)
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
(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.)