我有一個當前正在使用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