0

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:

  1. User tries to create new Task
  2. User asked to input Assignment name
  3. Search through database for Assignment

    a. If a Assignment is found, show details, ask user to confirm

    b. If not found, ask user to create a new Assignment

  4. With Assignment attached, ask user to input Task 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
Community
  • 1
  • 1
thisisdee
  • 878
  • 3
  • 16
  • 41
  • Can you give us a bit more insight into the nature of these parent/child models, i.e. what they represent? Sometimes tweaking a relationship slightly can suddenly give you a more natural and less painful flow. – janfoeh Feb 14 '15 at 12:33
  • @janfoeh, edited question above, not sure if it's clear enough. – thisisdee Feb 14 '15 at 13:00
  • Thanks! That does help. What does an `Assignment` do? If Tasks cannot be without Assignments and vice versa, that sounds like a job for a join model and `has_many :through`. – janfoeh Feb 14 '15 at 13:15
  • @janfoeh not sure what you mean by "What does an `Assignment` do?" It's just a Model that holds the common information for Tasks (name, description, type, etc), while a Task is specific to each User. If I understand what you meant correctly, a `Task` should be the join model of `Assignment` and `User`? – thisisdee Feb 14 '15 at 13:34
  • I would have expected it to be the other way around - `Task` describes what to do ("clean the living room"), and `Assignment` describing who owns that task. Doesn't that make more sense? – janfoeh Feb 14 '15 at 13:37
  • In that case, `Assignment` would be a simple join model: `Assignment belongs_to :user ; belongs_to :task` - `User has_many :assignments ; has_many :tasks, through: :assignments` - `Task has_one :assignment; has_one :user, through: :assignment` – janfoeh Feb 14 '15 at 13:43
  • @janfoeh Hmm it is the other way around, but I think that's easily fixable. I added my models and relationships above, to be clearer. Is what I wanted to achieve feasible? – thisisdee Feb 14 '15 at 13:43
  • 1
    Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/70912/discussion-between-janfoeh-and-dee). – janfoeh Feb 14 '15 at 13:44

0 Answers0