On Apr 7, 1:28 pm, Marcelo de Moraes Serpa <celose...@gmail.com>
wrote:

>
> So let's take this code
>
> p = Product.find(1)
>
> What I need is a specific products_suppliers related to specific combo of
> (supplier_id,product_id) keys, so I can get the supplier_type data related
> to this product+supplier. However, I the has_many relationship in Supplier
> reaturns all the products_suppliers that has supplier_id == the id of this
> supplier -- To get the specific products_supplier I would also need the id
> of the product that owns this supplier, but I can't get it. So, the first
> thing I though was something like has_one :through. That's why I said, a
> has_one relationship through a link table. The thing is, I need the
> product_id that owns this supplier from the supplier model. I might as well
> just leave AR and do some raw SQL, or maybe use sql :select statemente in
> the association to fetch the type from the link table into the association.
>
> Does that make sense?
>

Maybe this is too obvious, but given the example above, couldn't you
just use:

p.products_suppliers.find_by_supplier_id(some_supplier_id)

That will give you the supplier that you're looking for.

BTW, I'd recommend a different name for the supplier_type column -
that pattern (same as the foreign key, with _type instead of _id) is
the Rails convention for single table inheritance, which is NOT what
you're looking for here. It shouldn't cause a problem, but the AR
association code is known to sometimes get really weird indigestion
from that situation...

Hope this helps,

--Matt Jones
--~--~---------~--~----~------------~-------~--~----~
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@googlegroups.com
To unsubscribe from this group, send email to 
rubyonrails-talk+unsubscr...@googlegroups.com
For more options, visit this group at 
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to