As far as I know, areas cannot be applied to such associations. However, you can specify conditions that should only apply to download requests. Therefore, with a little refactoring, you may have a method that only created the conditions that you currently define in your area:
def self.filter_by(options) condition = options[:user_id].nil? ? "true" : "entries.user_id = #{options[:user_id]}" condition += options[:from_date].nil? ? "" : " AND entries.entry_date >= '#{options[:from_date]}'" condition += options[:to_date].nil? ? "" : " AND entries.entry_date <= '#{options[:to_date]}' condition end
or a little more rubyesque:
def self.filter_by(options) conditions = [] conditions << "entries.user_id = #{options[:user_id]}" unless options[:user_id].nil? conditions << "entries.entry_date >= '#{options[:from_date]}'" unless options[:from_date].nil? conditions << "entries.entry_date <= '#{options[:to_date]}'" unless options[:to_date].nil? conditions.join(" AND ") end
and then bind this method to your active boot:
@projects = current_user.projects.includes(:entries, :to_dos =>[:entries, :tasks => :entries].where(Entry.filter_by(options))
and also reuse it in your area if you need it:
scope :filtered_list, lambda { |options| where(Entry.filter_by(options)) }
Disclaimer: None of these have been tested with your actual model definitions, but it works fine with some pretty equivalent ones that I was lying on.
Also note that if the filter parameters ultimately come from the client side, your state is vulnerable to SQL injection.
Behind the scenes, Rails uses JOIN to load the relevant data, so this is what you need to know about. It can be good (slightly fewer requests) or bad (if your indexing is suboptimal). This is probably why guide says the following:
Even though Active Record allows you to set conditions for those who wish, loaded associations, like associations, it is recommended that you use joins instead.