Associations Question

I have a three table construct. A “Users” table. Users have a one-to-
one relationship with a “Carts” table for saving the contents of a
shopping cart to the DB. I have a “Products” table for holding
product information. The Carts have a many-to-many relationship with
the Products. (One cart might have more than one product in it, and
one product might be represented in more than one user’s cart.)

What association would be best to describe this relationship? And
what would the migrations and model statements look like?



2009/8/1 Andrew P. [email protected]:

User has_one Cart
Cart belongs_to User, has_and_belongs_to_many Products
Product has_and_belongs_to_many Carts

See the rails guides Getting Started and ActiveRecord Relationships at for more details.