我有一个当前正在使用Articles的注释模型。我现在想让用户能够评论Coffeeshop评论。我能否使用相同的评论表,或者我应该有一个单独的评论表(感觉很好)。我一直没有用RoR(几个星期)来构建,所以仍然试图掌握基础知识。Rails中的多个belongs_to模型
我会窝他们routes.rb中(以及如何)
resources :coffeeshops do
resources :articles do
resources :comments
end
或
resources :coffeeshops do
resources :comments
end
resources :articles do
resources :comments
end
我的模式是这样的:
用户
class User < ApplicationRecord
has_many :comments
end
评论
class Comment < ApplicationRecord
belongs_to :user
belongs_to :article
belongs_to :coffeeshop
end
文章
class Article < ApplicationRecord
has_many :comments, dependent: :destroy
end
咖啡店
class Coffeeshop < ApplicationRecord
has_many :comments, dependent: :destroy
我再假设我需要一个外键,以配合用户和评论在一起,然后还有评论文章/咖啡店。
好的。用户仍然只有'has_many:comments'? –
@SimonCooper:是的。当然是用':as::commentable'。 –
就像塞尔吉奥已经提到的一样;是。我编辑了包含用户模型的答案。 – Laurens