Answer the question
In order to leave comments, you need to log in
How to create a child object when linking self reference?
There is a self-attached
Task model :
class Task < ActiveRecord::Base
has_many :subtasks, class_name: 'Task', foreign_key: "parent_id"
belongs_to :parent, class_name: 'Task'
belongs_to :user
belongs_to :project
end
class CreateTasks < ActiveRecord::Migration
def change
create_table :tasks do |t|
t.string :title
t.text :description
t.string :priority
t.string :status
t.date :scheduled
t.date :deadline
t.integer :user_id
t.integer :project_id
t.references :parent, index: true
t.timestamps null: false
end
end
end
@task = Task.find(params[:id])
@task.subtasks.new
@task.save
Answer the question
In order to leave comments, you need to log in
Answer:
The 1st controller is enough - TasksController.
We add the "accepts_nested_attributes_for" parameter to the model, which passes additional parameters when creating an object.
class Task < ActiveRecord::Base
has_many :subtasks, class_name: 'Task', foreign_key: "parent_id"
belongs_to :parent, class_name: 'Task'
accepts_nested_attributes_for :subtasks, allow_destroy: true
belongs_to :user
belongs_to :project
end
def task_params
params.require(:task).permit(:title, :description, :priority, :status, :scheduled, :deadline, subtasks_attributes: [:title])
end
<%= simple_form_for @task do |t| %>
<%= t.simple_fields_for :subtasks, @task.subtasks.build do |f| %>
<div class="form-inputs">
<%= f.input :title %>
</div>
<div class="form-actions">
<%= f.button :submit %>
</div>
<% end %>
<% end %>
If you are following RailsWay, then you should have 2 controllers. And if you do resource routing, then put subtasks in tasks.
If you move away from RailsWay towards designing an API for ajax-exchange of json data, then do what is convenient for you.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question