views:

370

answers:

1

I have a model, target, that holds a number of records that are timestamped. On the corresponding controller, I list the months of those records by doing the following:

In models/target.rb

def month
   self.recorded_on.strftime('%B')
end

In controllers/targets_controller.rb

@records = Target.find :all

In views/targets/index.html.haml

%ul
  - @records.group_by(&:month).sort.each do |month, data|
    %li= link_to month, ''

That all works great for listing the available months for the records that I have. Next, I want to be able to click on the month and get a report of all the records for that month, at the following path generated with year and the month: /targets/2009/04

How would I do this?

+2  A: 
John Topley
I get this error for the named_scope's bit: SQLite3::SQLException: no such function: YEAR: SELECT * FROM "targets" WHERE ((MONTH(recorded_on) = NULL) AND (YEAR(recorded_on) = NULL)) ... ?
neezer
I didn't realise you were using SQLite because you didn't specify. You just need to use the appropriate SQL then. See http://www.sqlite.org/lang_datefunc.html
John Topley
My bad about not specifying that: also, I think you mis-typed the route above... should start "map.targets_by_month", I think. Also, along those lines, the route only seems to work with the year ("/targets/2009"); if I supply a month as well, I get an Unknown Route error ("/targets/2009/4"). Any idea why?
neezer
Good catch, I've fixed the route declaration. Does it work with a two digit month?
John Topley
Yeah, I figured out what I was doing wrong: I had the route declaration after the default routes ("map.connect ':controller/:action/:id' / map.connect ':controller/:action/:id.:format' "), when they need to come before.
neezer
Sorry, I should have mentioned that. I'll make an edit.
John Topley