現在、記事を扱っているコメントモデルがあります。私はユーザーがコーヒーショップのレビューにコメントできるようにしたいと思っています。私は同じコメント表を使用することができますか、別のものを持っているべきですか(ジャンク感)。私は長い間、RoR(数週間)で構築してきたので、まだ基礎を掴もうとしています。複数のbelongs_toモデルのレール
routes.rbを(どのように)でしまうIの巣それらを
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
私はその後、私は一緒にユーザーとコメントを結びつけるために、外部キーを必要とすると仮定し、としています記事/コーヒーショップにコメントしてください。
OKです。そして、ユーザーはまだ 'has_many:comments'を持っていますか? –
@SimonCooper:そうです。もちろん、 'as::commentable'を使用します。 –
セルジオは既に言及したように;はい。 Userモデルを含める答えを編集しました。 – Laurens