[Rails] Using with_scope inside an association extension

John Ward
Wed, 23 Aug 2006 08:44:38 -0000

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





--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk
-~----------~----~----~----~------~----~------~--~---