Rails4:一个子模型可以belong_to两个不同的父模型
Rails 4: can a child model belong_to two different parent models
在我最初的 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
我刚刚向应用添加了一个新的 Ad
模型:
Ad
belongs_to :calendar
现在我想允许用户对广告记录发表评论。
我可以使用我现有的 Comment
模型并执行以下操作:
Ad
belongs_to :calendar
has_many :comments
Comment
belongs_to :post
belongs_to :user
或者我是否需要创建一个独特的 "Comment" 模型,例如我会调用 AdComments
或 Feedback
?
我们不需要使用任何新模型,您可以使用 polymorphic
重构当前的 Comment 模型
所以,一条评论永远属于一个用户,属于一个post或广告
您需要使用 polymorphic associations。大概是这样的:
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
迁移看起来像:
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
我想您已经有了评论 table,所以您应该将 table 更改为
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
另请注意,如果您有实时数据,则应将所有现有评论的 commentable_type
字段更新为 Post
。您可以在迁移中或从控制台执行此操作。
Comment.update_all commentable_type: 'Post'
在我最初的 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
我刚刚向应用添加了一个新的 Ad
模型:
Ad
belongs_to :calendar
现在我想允许用户对广告记录发表评论。
我可以使用我现有的 Comment
模型并执行以下操作:
Ad
belongs_to :calendar
has_many :comments
Comment
belongs_to :post
belongs_to :user
或者我是否需要创建一个独特的 "Comment" 模型,例如我会调用 AdComments
或 Feedback
?
我们不需要使用任何新模型,您可以使用 polymorphic
重构当前的 Comment 模型所以,一条评论永远属于一个用户,属于一个post或广告
您需要使用 polymorphic associations。大概是这样的:
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
迁移看起来像:
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
我想您已经有了评论 table,所以您应该将 table 更改为
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
另请注意,如果您有实时数据,则应将所有现有评论的 commentable_type
字段更新为 Post
。您可以在迁移中或从控制台执行此操作。
Comment.update_all commentable_type: 'Post'