Alright, database stuff is turning out to be my rails weakpoint.
I have 3 models I am trying to search across: User, Invoice, Payment
User has_many invoices and Invoice has_many payments.
I am trying to create a search that will find all of the payments for the current_user across a given date range.
This is how I have done this so far
invoices = current_user.invoices
payments = invoices.inject([]) {|arr,x| arr += x.payments.where("payment_date <= ? and payment_date >= ?", '2011-02-01', '2011-01-01')}
This seems insane to me. I’m sure there must be a way of getting this out of the database directly without iterating through results. Any ideas? Cheers in advance!
You should use something like :
You probably want to read the Ruby On Rails Guides about active record query
UPDATE:
From the guide linked, a more cleaner syntax will be :
UPDATE 2, another solution :
In your model
User:To perform the query :