Let’s say we have the following models:
Product < ActiveRecord::Base
has_many :suppliers, :through => :products_supplier
Supplier < ActiveRecord::Base
has_many :products, :through => :products_supplier
ProductsSupplier < ActiveRecord::Base
Let’ say we have this data in the products_suppliers table:
| supplier_id | product_id |
| 1 | 2 |
| 1 | 1 |
| 2 | 2 |
| 2 | 1 |
So when I do this:
@suppliers = Product.find(1).suppliers, it will return an array with the
suppliers with id 1 and 2.
Let’s say I do this:
@supplier = @suppliers #Which is the supplier with id = 1, the one
fetched from the (supplier_id = 1, product_id = 2) record in the
products_suppliers table. Ok, now I got a single instance of a supplier.
What I want is to get the product_id it was associated with in the
products_suppliers table. (product_id = 2), might be missing something
simple, but I can’t seem to find a way to do that.
Any help appreciated!