我有2个模型,例如任务模型和任务相关模型 任务有许多父任务和子任务。Rails 3:与多态关联的多对多关系
已新增下列协会 -
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表中,relation_type为'Task',但我想存储relati on_type作为父任务的“父”和子任务的“子”。
任何人都可以请帮我在这。
这个模型看起来不必要的复杂 - 你能澄清(用文字,而不是代码)一个任务需要与什么关联吗? – PinnyM
想要将任务模型与任务关联起来作为父任务和子任务。需要将此关联存储在task_relation模型中,哪个任务是父任务,哪个任务是子任务。 –
预计每个任务会有多个父母和孩子吗? – PinnyM