Note that there are some explanatory texts on larger screens.

plurals
  1. PORails 3: Many to many relationship with polymorphic association
    primarykey
    data
    text
    <p>I have 2 models e.g task model and task_relation model Task has many parent tasks and child tasks.</p> <p>Have added following associations -</p> <p><strong>Task.rb</strong></p> <pre><code> has_many :from_tasks, :as =&gt; :relation, :class_name =&gt; "TaskRelation", :foreign_key =&gt; "task_from_id", :source =&gt; :parent, :conditions =&gt; {:relation_type =&gt; 'Parent'}, :dependent =&gt; :destroy has_many :to_tasks , :as =&gt; :relation, :class_name =&gt; "TaskRelation", :foreign_key =&gt; "task_to_id", :source =&gt; :child, :conditions =&gt; {:relation_type =&gt; 'Child'}, :dependent =&gt; :destroy has_many :child_tasks, :through =&gt; :from_tasks, :dependent =&gt; :destroy has_many :parent_tasks, :through =&gt; :to_tasks, :dependent =&gt; :destroy accepts_nested_attributes_for :to_tasks, :reject_if =&gt; :all_blank, :allow_destroy =&gt; true accepts_nested_attributes_for :from_tasks, :reject_if =&gt; :all_blank, :allow_destroy =&gt; true </code></pre> <p><strong>TaskRelation.rb</strong></p> <pre><code> belongs_to :parent_task, :class_name =&gt; "Task", :foreign_key =&gt; "task_from_id" belongs_to :child_task, :class_name =&gt; "Task", :foreign_key =&gt; "task_to_id" belongs_to :relation, :polymorphic =&gt; true </code></pre> <p>When I save task form, it also saves parent_tasks and child tasks in task_relations table with relation_type as 'Task' but I want to store relation_type as 'Parent' for parent tasks and 'Child' for child tasks.</p> <p>Can anyone please help me on this.</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload