Rails 4:子模型可以属于两个不同的父模型吗

Thi*_*ent 1 model-view-controller activerecord ruby-on-rails relational-database ruby-on-rails-4

在我最初的 Rails 4 应用程序中,我有以下模型:

User
has_many :administrations
has_many :calendars, through: :administrations
has_many :comments

Calendar
has_many :administrations
has_many :users, through: :administrations
has_many :posts
has_many :comments, through: :posts

Administration
belongs_to :user
belongs_to :calendar

Post
belongs_to :calendar
has_many :comments

Comment
belongs_to :post
belongs_to :user
Run Code Online (Sandbox Code Playgroud)

我刚刚向Ad应用程序添加了一个新模型:

Ad
belongs_to :calendar
Run Code Online (Sandbox Code Playgroud)

现在我想允许用户写关于广告记录的评论。

我可以使用我现有的Comment模型并执行以下操作:

Ad
belongs_to :calendar
has_many :comments

Comment
belongs_to :post
belongs_to :user
Run Code Online (Sandbox Code Playgroud)

或者我是否需要创建一个独特的“评论”模型,例如我会调用它AdComments还是Feedback

Mih*_*ner 6

您需要使用多态关联。这方面的事情:

class Comment < ActiveRecord::Base
  belongs_to :commentable, polymorphic: true
end

class Ad < ActiveRecord::Base
  has_many :comments, as: :commentable
end

class Product < ActiveRecord::Base
  has_many :comments, as: :commentable
end
Run Code Online (Sandbox Code Playgroud)

迁移看起来像:

class CreateComments < ActiveRecord::Migration
  def change
    create_table :comments do |t|
      t.references :commentable, polymorphic: true, index: true
      t.timestamps null: false
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

我猜你已经有了评论表,所以你应该用

class ChangeComments < ActiveRecord::Migration
  def change
    change_table :comments do |t|
      t.rename :post_id, :commentable_id 
      t.string :commentable_type, null: false
    end
  end
end
Run Code Online (Sandbox Code Playgroud)

另请注意,如果您有实时数据,则应将commentable_type所有现有评论的字段更新为Post. 您可以在迁移中或从控制台执行此操作。

Comment.update_all commentable_type: 'Post'
Run Code Online (Sandbox Code Playgroud)