I don't think a delegate would work. Okay, just to recap on the
database structure:

products
| id | name | price |
|  1 |     a   |  2.50 |
|  2 |     b   |  1.50 |

suppliers
| id | name |
|  1 |  s_a  |
|  2 |  s_b  |

products_suppliers
| id | supplier_id | product_id | supplier_type |
| 1  |       1         |     1          |   "vendor"      |
| 2  |       1         |      2         | "manufacturer"|

Firstly, I don't think that 'products_suppliers' is a good name since
this form is only a requirement for HABTM to work. It should be more
descriptive of the relationship, but your call.

Okay, so I believe that you want to do something like this:

p = Product.first

p.vendors # => [#< Supplier id: 1, name: "s_a">, #<Supplier id: 2,
name: "s_b">]
p.manufacturer # => #< Supplier id: 1, name: "s_a">

You might want to try this:

class Product < ActiveRecord::Base
  has_many :products_suppliers
  has_many suppliers, :through => products_suppliers

  named_scope :vendors, :conditions => ["supplier_type = ?", "vendor"]
  named_scope :manufacturer, :conditions => ["supplier_type = ?",
"manufacturer"]
end

This does not restrict the manufacturer to a one-to-many relationship,
so you will have to ensure that each product will have only one
manufacturer (you can do this pretty easily with some validations).

On Apr 8, 11:14 am, Matt Jones <al2o...@gmail.com> wrote:
> 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