多重多对多关系Rails



我有用户和餐馆

模拟收藏夹我有一个餐馆用户连接表,我用它来模拟用户是否"收藏"了一家餐馆。

对评审进行建模我有一个评论表,引用user_id和restaurant_id与其他一些字段。我应该在这里使用has_many_through吗?那会是什么样子?

由于这里有多个多对多关系,我只是想知道我是否做得对。

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

最新更新