Reputation: 851
I'm looking to create a simple relationship model with the following:
I have 2 main items. Product
/ WishList
The relationship between them is pretty straight forward a WishList
has_many :products
and a Product
belongs_to_many :wishlists
I understand that there isn't a belongs_to_many
association and as simple as this may be I can't seem to wrap my head around the right relationship model.
If someone could suggest a nice way to achieve this it would be much appreciated.
Thanks.
Upvotes: 2
Views: 118
Reputation: 17737
You have a choice between a has_many :through and a has_and_belong_to_many relation.
Pick the first one if you need to attach other attributes to the model that will be the bridge (for instance a position, if you want to order your wishlist), or the second one otherwise (more about that here)
So it could look like so
class Product < ActiveRecord::Base
has_many :items
has_many :wishlists, through: :items
end
class Item < ActiveRecord::Base
belongs_to :product
belongs_to :wishlist
end
class Wishlist < ActiveRecord::Base
has_many :items
has_many :products, through: :items
end
Upvotes: 2