views:

613

answers:

5

I have a projects resource that has many tasks. I want to ensure that every task has a project_id by adding validates_presence_of :project_id to the tasks model.

However, when creating a new project with tasks, the project_id won't be available until the record saves, therefore I can't use validates_presence_of :project_id.

So my question is, how do I validate presence of project_id in the task model? I want to ensure every task has a parent.

...

class Project < ActiveRecord::Base

  has_many :tasks, :dependent => :destroy
  accepts_nested_attributes_for :tasks, :allow_destroy => true

...

class Task < ActiveRecord::Base

 belongs_to :project
 validates_presence_of :project_id
A: 

Your Project class must define

accepts_nested_attributes_for :tasks

See Nested Model Form on Railscasts for more details on how to make the form.


EDIT:

In your form you should have something like this:

_form.html.erb

<% form_for @project do |f| %> 
    # project fields...
    <% f.fields_for :tasks do |builder| %>
        <%= render 'task_fields', :f => builder %>
    <% end %>
    <p><%= link_to_add_fields "Add task", f, :tasks %></p>
    <%= f.submit %>
<% end %>

_task_fields.html.erb

<%= f.label :name, "Task name:" %>
<%= f.text_field :name %>
# task fields...
<%= link_to_remove_fields "Delete task", f, :tasks %>

link_to_add_fields and link_to_remove_fields are methods defined in application_helper to add/delete fields dynamically.

True Soft
The question is how to make project_id mandatory for tasks.
deb
I posted an example of the code to help clarify.thanks!
deb
From what I know, if a `Task` `belongs_to :project`, `project_id` can never be 0/NULL
True Soft
hmn - good question. So when they are saved together it doesn't create projects first, then the tasks?
ro
Well, it first saves the project, then the resulted id goes to the `project_id` of the task
True Soft
+1  A: 

Maybe I don't understand something, but it looks like you are trying to cheat rails. Why don't you just do like this:

class Task < ActiveRecord::Base
  belongs_to :project
  validate_presence_of :project
end
bsboris
+1  A: 

Take a look at this:

https://rails.lighthouseapp.com/projects/8994/tickets/2815-nested-models-build-should-directly-assign-the-parent

One thing I have done in the past is add: validates_presence_of :parent_id, :on => :update. Not great but it helps tighten the net a little.

tsdbrown
Fantastic! This works like a charm.
molf
+2  A: 

Your code works:

  • If you validates_presence_of :project, then as long as the project is there, it will validate. But if your project is unsaved, you could still save the task.
  • If you validates_presence_of :project_id, then the integer must be there, indicating a saved value.

Here's rSpec that proves the point. If you validate :project_id, you can't save a task without saving the Project.

class Task < ActiveRecord::Base
  belongs_to :project
end

/specs/model_specs/task_spec.rb

require File.dirname(__FILE__) + '/../spec_helper'

describe Task do

  before(:each) do 
    @project = Project.new
  end

  it "should require a project_id, not just a project object" do
    task = Task.new
    task.project = @project
    Task.instance_eval("validates_presence_of :project_id")
    task.valid?.should == false
  end

  it "should not be valid without a project" do
    task = Task.new
    task.project = @project
    Task.instance_eval("validates_presence_of :project")
    task.valid?.should == false
    task.save.should == false
  end

end
Jesse Wolgamott
I want to mark your answer as correct but the check mark is gone :/
deb
Thanks deb! Maybe it'll come back soon.
Jesse Wolgamott
+1  A: 

See here for the definitive answer :

class Project < ActiveRecord::Base

  has_many :tasks, :dependent => :destroy, :inverse_of => :project
  accepts_nested_attributes_for :tasks, :allow_destroy => true

class Task < ActiveRecord::Base

 belongs_to :project
 validates_presence_of :project

Not so elegant if you ask me... It should transparently validate...

gamov
thanks I'll look into "inverse_of"
deb