[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

Rails: has_and_belongs_to_many

Pedro Santos

2/12/2005 9:29:00 PM

Hi,

I am experimenting with Rails, and I have the following question regarding
ActiveRecord.

If I want to model a situation where a many to many relationship has some
fields associated with it, is there any way to do it with ActiveRecord?

For instance, if several stores have product inventory I would have a many
to many relation between the "products" table and the "stores" table, with
the quantity stored in the table "stores_products".


Thanks,

Il Doge

2 Answers

Cameron McBride

2/13/2005 3:38:00 AM

0

Greetings,

> I am experimenting with Rails, and I have the following question regarding
> ActiveRecord.

first off, the rails list might usually be more helpful (quicker) with
rails questions rather than ruby-talk.

> If I want to model a situation where a many to many relationship has some
> fields associated with it, is there any way to do it with ActiveRecord?
>
> For instance, if several stores have product inventory I would have a many
> to many relation between the "products" table and the "stores" table, with
> the quantity stored in the table "stores_products".

um, yes. but like your subject says, it's the
has_and_belongs_to_many part that does EXACTLY what you're talking
about. in good faith, here is the link:
http://ar.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.ht...

or look at the 'associations.rb' in the examples directory of AR.

Cameron


Pedro Santos

2/13/2005 10:33:00 AM

0

Thanks, I had missed the

collection.push_with_attributes(object, join_attributes)

method in the "has_and_belongs_to_many" meta-method description.

Pedro

in article dcedf5e205021219371950808d@mail.gmail.com, Cameron McBride at
cameron.mcbride@gmail.com wrote on 05/02/13 3:38 AM:

> Greetings,
>
>> I am experimenting with Rails, and I have the following question regarding
>> ActiveRecord.
>
> first off, the rails list might usually be more helpful (quicker) with
> rails questions rather than ruby-talk.
>
>> If I want to model a situation where a many to many relationship has some
>> fields associated with it, is there any way to do it with ActiveRecord?
>>
>> For instance, if several stores have product inventory I would have a many
>> to many relation between the "products" table and the "stores" table, with
>> the quantity stored in the table "stores_products".
>
> um, yes. but like your subject says, it's the
> has_and_belongs_to_many part that does EXACTLY what you're talking
> about. in good faith, here is the link:
> http://ar.rubyonrails.org/classes/ActiveRecord/Associations/ClassMet...
> M000011
>
> or look at the 'associations.rb' in the examples directory of AR.
>
> Cameron
>
>