将一个对象限制为一个所有权 ID
Limiting an object to one ownership ID
我有一个 Flag
模型,它有可能属于 Topic
、Post
或 Review
。话虽如此,Flag
模型具有所有三个对应的 ID。
class Flag < ActiveRecord::Base
belongs_to :topic
belongs_to :post
belongs_to :review
end
class Topic < ActiveRecord::Base
has_many :flags
end
class Post < ActiveRecord::Base
has_many :flags
end
class Review < ActiveRecord::Base
has_many :flags
end
归根结底要标记 post,我如何确保此后不存在其他 ID(topic_id
、review_id
)?我想为这三个中的每一个创建特定的标志模型,但这条路线似乎太重复了。模型验证是我唯一明智的选择吗?如果是这样,它会是什么样子?更重要的是,我是否以正确的方式进行此关联设置?
使用 polymorphic associations 怎么样?
试试:
class Flag < ActiveRecord::Base
belongs_to :flaggable, polymorphic: true
end
class Topic < ActiveRecord::Base
has_many :flags, as: :flaggable
end
class Post < ActiveRecord::Base
has_many :flags, as: :flaggable
end
class Review < ActiveRecord::Base
has_many :flags, as: :flaggable
end
class UpdateAssociationsOnFlags < ActiveRecord::Migration
def change
remove_index :flags, :topic_id
remove_column :flags, :topic_id
remove_index :flags, :post_id
remove_column :flags, :post_id
remove_index :flags, :review_id
remove_column :flags, :review_id
add_column :flags, :flaggable_id, :integer
add_column :flags, :flaggable_type, :string
add_index :flags, :flaggable_id
end
end
编辑,回复问题作者评论:
您可以添加一个不同的关联:
class Flag < ActiveRecord::Base
belongs_to :flaggable, polymorphic: true
belongs_to :reporter, class_name: 'User', foreign_key: :reporter_id
end
class User < ActiveRecord::Base
has_many :reported_flags, class_name: 'Flag', foreign_key: :reporter_id
end
class AddUserForeignKeyToFlags < ActiveRecord::Migration
def change
add_column :flags, :reporter_id, :integer
add_index :flags, :reporter_id
end
end
我有一个 Flag
模型,它有可能属于 Topic
、Post
或 Review
。话虽如此,Flag
模型具有所有三个对应的 ID。
class Flag < ActiveRecord::Base
belongs_to :topic
belongs_to :post
belongs_to :review
end
class Topic < ActiveRecord::Base
has_many :flags
end
class Post < ActiveRecord::Base
has_many :flags
end
class Review < ActiveRecord::Base
has_many :flags
end
归根结底要标记 post,我如何确保此后不存在其他 ID(topic_id
、review_id
)?我想为这三个中的每一个创建特定的标志模型,但这条路线似乎太重复了。模型验证是我唯一明智的选择吗?如果是这样,它会是什么样子?更重要的是,我是否以正确的方式进行此关联设置?
使用 polymorphic associations 怎么样?
试试:
class Flag < ActiveRecord::Base
belongs_to :flaggable, polymorphic: true
end
class Topic < ActiveRecord::Base
has_many :flags, as: :flaggable
end
class Post < ActiveRecord::Base
has_many :flags, as: :flaggable
end
class Review < ActiveRecord::Base
has_many :flags, as: :flaggable
end
class UpdateAssociationsOnFlags < ActiveRecord::Migration
def change
remove_index :flags, :topic_id
remove_column :flags, :topic_id
remove_index :flags, :post_id
remove_column :flags, :post_id
remove_index :flags, :review_id
remove_column :flags, :review_id
add_column :flags, :flaggable_id, :integer
add_column :flags, :flaggable_type, :string
add_index :flags, :flaggable_id
end
end
编辑,回复问题作者评论:
您可以添加一个不同的关联:
class Flag < ActiveRecord::Base
belongs_to :flaggable, polymorphic: true
belongs_to :reporter, class_name: 'User', foreign_key: :reporter_id
end
class User < ActiveRecord::Base
has_many :reported_flags, class_name: 'Flag', foreign_key: :reporter_id
end
class AddUserForeignKeyToFlags < ActiveRecord::Migration
def change
add_column :flags, :reporter_id, :integer
add_index :flags, :reporter_id
end
end