Given the following models, using rails 2.1:
class Product < ActiveRecord::Base
has_many :from_sources, :class_name => 'ProductSource', :foreign_key => 'to_product_id'
has_many :to_sources, :class_name => 'ProductSource', :foreign_key => 'from_product_id'
has_many :from_products, :through => :from_sources
has_many :to_products, :through => :to_sources
end
class ProductSource < ActiveRecord::Base
belongs_to :from_product
belongs_to :to_product
end
class Supplier < ActiveRecord::Base
has_many :products
end
I really can’t find a way to filter a supplier’s products that comes from another supplier.
Let me give and example with some data
- Supplier SA has products SAPA, SAPB
- Supplier SB has products SBPA, SBPB
- Supplier SC has products SCPA (redistributed from SBPB), SCPB (redistributed from SAPB)
I want to filter :products from supplier SC with only products that comes from SB.
Thanks!
not sure if i understand well your schema, but i think you will at least need your Product to belong_to a supplier, so you can filter the products table based on which supplier provides it:
you can now :
so the actual SQL query should look like:
Sorry, but im not really familiar to rails 2 query interface (rails 3 is so sweet),so i can’t tell more, but i think that is the right logic.