Rails: has_and_belongs_to_many

P

Pedro Santos

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
 
C

Cameron McBride

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.html#M000011

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

Cameron
 
P

Pedro Santos

Thanks, I had missed the

collection.push_with_attributes(object, join_attributes)

method in the "has_and_belongs_to_many" meta-method description.

Pedro
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,167
Messages
2,570,911
Members
47,453
Latest member
MadelinePh

Latest Threads

Top