named_scope is your friend
class Download < ActiveRecord::Base
belongs_to :file
named_scope :between, lambda{ |start_date, end_date| { :conditions
=> ["created_at >= ? AND created_at < ?", start_date,
end_date] } }
named_scope :from_file, lambda { |file| { :conditions => ["file_id
= ?", file.id] } }
end
Download.between("2008-10-01".to_date,
"2008-11-01".to_date).from_file(file).count
On Oct 21, 1:25 pm, Neal L
<neal.lo...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>
wrote:> Hi all,
>
> Suppose you have two models -- one for Files and anther for Downloads
> -- such that a file has_many => :downloads
>
> If you wanted to get a count of the downloads for the current month
> and the two previous months, is there a way to get that data from one
> query? Something like:
>
> @file.downloads_for_month( 2008, 10 )
>
> where 2008 is the year and 10 is the month...
>
> Thanks!
--~--~---------~--~----~------------~-------~--~----~
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
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---