Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 864419
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T09:28:29+00:00 2026-05-15T09:28:29+00:00

I have a projects resource that has many tasks. I want to ensure that

  • 0

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
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T09:28:29+00:00Added an answer on May 15, 2026 at 9:28 am

    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
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have project model that has many tasks. Both project and tasks can have
Suppose I have the classes/models Projects (has many lists) Lists I want to allow
I have a project model that has_many :tasks . I added a nested resource
I have a Discussion (polymorphic resource) that can belong to Project, Task and Subtask.
Say that I have two resources, Project and Task. A Project can have many
I have a Maven project that is a child project. It has many sibling
I have many projects I work on normally, but I have one that consumes
I have an Eclipse plugin project that has several file resources that I would
Resource dictionary 03-11-2010 10:56 AM | I have two different projects. I linked a
I want to XML-Serialize a complex type (class), that has a property of type

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.