Eager loading of association extensions

Hello all,

Is it possible to do eager loading of association extensions?

That is, the following code produces one SQL query:

cat = ProductCategory.find(:first, :include => :pricing_rules)

cat.pricing_rules

But if in ProductCategory I have an association extension like this:

has_many :pricing_rules do

  def applicable()

    find(:all).select(&:applicable?)

  end

end

the code

cat = ProductCategory.find(:first, :include => :pricing_rules)

cat.pricing_rules.applicable

will produce 2 queries anyway, :include is useless here.

I see no other way that to add another association:

has_many :applicable_pricing_rules :conditions => ‘appliccable is true’

and then use it:

cat = ProductCategory.find(:first, :include => :applicable_pricing_rules)

cat.applicable_pricing_rules

But maybe there’s a way to eagerly load an association extension?

There is no way. The extra association route would also be more efficient because you only query the database for the relevant records rather than getting all of them and then throwing away some. But you could rewrite your association so that it uses pricing_rules, ie

def applicable   load_target.select(&:applicable?) end

Fred