Khadijah Celestine
Khadijah Celestine

Reputation: 537

Multiple Many to Many Relationships Rails

I have Users and Restaurants

To model Favorites I have a restaurants users join table that I'm using to model whether or not a user has "favorited" a restaurant.

To model Reviews I have a reviews table that references user_id and restaurant_id with some other fields. Should I use a has_many_through here? How would that look?

Since there are multiple many to many relationships here, I'm just wondering if I'm doing it right.

Upvotes: 0

Views: 60

Answers (1)

lokeshjain2008
lokeshjain2008

Reputation: 1901

class User < ActiveRecord::Base
   has_many :favourites
   has_many :reviews
   has_many :restaurants, through: :favourites
end

class Restaurant < ActiveRecord::Base
    has_many :reviews
end

class Favourite < ActiveRecord::Base
    belongs_to :user
    belongs_to :restaurant
end

class Reviews < ActiveRecord::Base
    belongs_to :restaurant
    belongs_to :user
end

Upvotes: 2

Related Questions