I want to perform an advanced query for the Task model (simplified versio:
class Task
belongs_to :user_task
belongs_to :customer
end
class UserTask
has_many :tasks
belongs_to: :user
attr_accessible :date
end
class Customer
has_many :tasks
end
What I want to build is a search form for de Task model with the followings fields:
- from_date (date field): all the tasks which corresponding UserTask is after from_date
- to_date (date field): all the tasks which corresponding UserTask is before to_date
- customers (collection as checkbox): all the tasks which corresponding customer is included in the customer selection.
I created a TaskSearch resource for saving searches and can imagine a big and ugly SQL string for querying the database but I am not sure this is the best way to do it.
Which would be the ActiveRecord approach?
Thanks in advanced.
Hopefully this will work. The thing is, as we are joining tables, you might get multiple results for the same task. You might have to add a distinct clause to the select() method, like this: