【问题标题】:Rails 3: Many to many relationship with polymorphic associationRails 3:与多态关联的多对多关系
【发布时间】:2013-12-13 11:43:02
【问题描述】:

我有 2 个模型,例如任务模型和任务关系模型 Task 有很多父任务和子任务。

已添加以下关联 -

Task.rb

  has_many :from_tasks, :as => :relation, :class_name => "TaskRelation", 
                        :foreign_key => "task_from_id", :source => :parent, 
                        :conditions => {:relation_type => 'Parent'}, :dependent => :destroy
  has_many :to_tasks , :as => :relation, :class_name => "TaskRelation", 
                       :foreign_key => "task_to_id", :source => :child, 
                       :conditions => {:relation_type => 'Child'}, :dependent => :destroy
  has_many :child_tasks, :through => :from_tasks, :dependent => :destroy
  has_many :parent_tasks, :through => :to_tasks, :dependent => :destroy

  accepts_nested_attributes_for :to_tasks, :reject_if => :all_blank, :allow_destroy => true
  accepts_nested_attributes_for :from_tasks, :reject_if => :all_blank, :allow_destroy => true

TaskRelation.rb

  belongs_to :parent_task, :class_name => "Task",  :foreign_key => "task_from_id"
  belongs_to :child_task, :class_name => "Task", :foreign_key => "task_to_id"
  belongs_to :relation, :polymorphic => true

当我保存任务表单时,它还会将 parent_tasks 和子任务保存在 task_relations 表中,其中关系类型为“任务”,但我想将关系类型存储为父任务的“父”和子任务的“子”。

谁能帮帮我。

【问题讨论】:

  • 这个模型看起来不必要的复杂——你能准确地(用文字而不是代码)阐明一个任务需要与什么相关联吗?
  • 想将任务模型与任务关联为父任务和子任务。需要将此关联存储在task_relation模型中,哪个任务是父任务,哪个是子任务。
  • 每个任务是否预计会有不止一个父母和孩子?
  • 是...任务可以有多个父任务和多个子任务

标签: ruby-on-rails ruby many-to-many has-many-through polymorphic-associations


【解决方案1】:

首先,删除 relation 多态关联 - 它不是必需的。现在,将您的 Task 模型修改为如下所示:

# This association relates to tasks that are parents of self
has_many :parent_task_relations, class_name: 'TaskRelation', foreign_key: 'child_task_id'
# And this association relates to tasks that are children of self
has_many :child_task_relations, class_name: 'TaskRelation', foreign_key: 'parent_task_id'

has_many :child_tasks, :through => :child_task_relations
has_many :parent_tasks, :through => :parent_task_relations

你应该完成了。

为了说明如何使用它 - 假设您有一个任务 a 并且需要将任务 B 分配为父任务,并将任务 C 分配为子任务。你可以这样做:

a.parent_tasks << b
a.child_tasks << c

这将对您的数据库产生与此代码相同的效果:

a.parent_task_relations.create(parent_task: b)
a.child_task_relations.create(child_task: c)

这与(对数据库)相同:

TaskRelation.create(parent_task: b, child_task: a)
TaskRelation.create(parent_task: a, child_task: c)

【讨论】:

  • 我之前已经添加了这个关系...但是根据它在应用程序中的使用,我需要修改为父子任务与原始任务之间的不同。
  • 例如。如果有 3 个任务 A、B 和 C,并且我将 B 作为父任务关联到 A,将 C 作为子任务关联到 A。可以有很多。所以为了区分谁是父母谁是孩子。我需要这种多态关联。
  • 不,你没有。在您给出的示例中,您可以简单地创建 2 个 TaskRelation,一个具有“父:B,子:A”,一个具有“父:A,子:C”。在第一个关系中,A 将被检测为孩子,而在第二个关系中,A 将被检测为父。
  • 延伸到这个关系,我需要与父子任务共享文件意味着任务A可以与任务B和任务C以及多个父子任务共享文件。在没有任何多态关联的情况下,我无法找到谁使用 task_relations 表与谁共享文件。
  • @SaileeReddy:多态关联适用于多个模型类需要与一个公共模型关联的情况。由于您(在此之前)仅连接任务模型(A、B 和 C),因此没有理由使其具有多态性 - 它可以是标准的 belongs_to *_id 字段。其次,在给出的示例中,B 和 C 之间是否存在真正的联系?也许更新您的问题并更广泛地解释您想要完成的工作将大大有助于澄清这一点......
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-08-25
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多