I'm fairly new to Rails, so I was wondering what the convention is something like this. I have 2 models, Assignment
and Task
with has_many/belongs_to relation. I want users to be able to create a Task
model, through searching for a Assignment
model or creating a new one. So my flow is:
- User tries to create new
Task
- User asked to input
Assignment
name Search through database for
Assignment
a. If a
Assignment
is found, show details, ask user to confirmb. If not found, ask user to create a new
Assignment
- With
Assignment
attached, ask user to inputTask
details
What I was wondering is, should all this be handled under TaskController (task#new)
or should step #3 be separately handled under AssignmentController (assignment#search)
? Also, where/when should I save the Assignment
model, after step #3 or wait until user goes through step #4 (so there are no assignments without tasks)?
Note: Users wouldn't be able to create Parent
model by itself, if this makes any difference.
The closest SO question I've found related to this issue is this one but there's no answer. Or is JS really the best way to achieve this?
EDIT: Relationship clarification
Parent is "Assignment" and Child is "Task" (renamed all "Parent" and "Child" references above). I want users to be able to list all assignments and list all tasks, but only create Task (no Assignment should exist without a Task attached to it). Tasks also belongs to Users. So User will create a Task that belongs to himself, and an Assignment (existing or new).
Currently, my routes.rb
looks like:
resources :assignments
resources :tasks
EDIT: Models
class Assignment < ActiveRecord::Base
has_many :tasks
end
class Task < ActiveRecord::Base
belongs_to :assignment
belongs_to :user
end
class User < ActiveRecord::Base
has_many :tasks
end