Hi All, I've created an association extension for a habtm association proxy. Inside of this extension I want to add some more joins.
module CategoryProductFinder def sold_by(user) joins = "INNER JOIN categories_product_classes ON product_classes.id = categories_product_classes.product_class_id" joins << " INNER JOIN sellers_product_classes ON categories_product_classes.product_class_id = sellers_product_classes.product_class_id"
self.with_scope({:find => {:joins => joins, :conditions => "sellers_product_classes.user_id = #{user.id}"}}) do find(:all, :select => "product_classes.*") end end end
My category class has the habtm association proxy with extension included
class Category < ActiveRecord::Base ... has_and_belongs_to_many :product_classes, :extend => CategoryProductFinder ... end
The intention is that this would allow me to write the following code
#assume user, and category are valid my_products = category.product_classes.sold_by(user)
*My Problem*
Unfortunately it doesn't work. The :joins inside the with_scope replaces the joins for the habtm association proxy. Does anyone know how to obtain the original joins SQL inside a scoped method. I could hardcode it but I imagine there's a more elegant way.
Regards, John