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 8002591
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 4, 20262026-06-04T16:24:25+00:00 2026-06-04T16:24:25+00:00

I have a nested model, Task which belongs to Project so in my model:

  • 0

I have a nested model, Task which belongs to Project so in my model:

class Task < ActiveRecord::Base
  belongs_to :project
  validates :project_id, :presence => true

In my projects model, I have:

class Project < ActiveRecord::Base
  has_many :tasks, :dependent => :destroy

Now, I have a form to create a new task where the user can specify project_id i.e. which project the task belongs to. So my form looks something like this:

    <%= form_for [@project, @task], :remote => true do |f| %>
        <%= f.label :title, "Title" %>
        <%= f.text_field :title %>
        <%= label_tag("Project") %>
        <%= select(nil, :project_id, (get_active_projects).collect {|p| [p.title, p.id]  }, {:prompt => 'Select Project'}) %>

        <%= f.label :description %>
        <%= f.text_area :description %>

       <div class="actions">
       <%= f.submit 'Save' %>
       </div>
    <% end %>

The problem I’m having with validation is that if the user didn’t select a project (project_id) in the form, I get an error saying that a project can’t be found before any validation can be done right? I want my validation enforcing the presence of a project_id to happen before there is an attempt to find the project.

def create
    @project = Project.find(params[:project_id])

    @task = @project.tasks.new(params[:task])
    @task.update_attributes(:status_id => 2)
    @task.save!
  • 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-06-04T16:24:26+00:00Added an answer on June 4, 2026 at 4:24 pm

    In your way you trying to find project in your method and if project_id is nil it will give that error. As it is has_many and belongs_to relationship it enough to save the project_id in Task you don’t have to create task through projects. As you are creating a task not many you dont need project object in form_for you can go like this

    <%= form_for @task, :remote => true do |f| %>
        <%= error_messages_for(@task) %>
        <%= f.label :title, "Title" %>
        <%= f.text_field :title %>
        <%= f.label :project_id %>
        <%= f.select(nil, :project_id, (get_active_projects).collect {|p| [p.title, p.id]  }, {:prompt => 'Select Project'}) %>  (just check the select syntax if i did something wrong)
    
        <%= f.label :description %>
        <%= f.text_area :description %>
    
        <div class="actions">
            <%= f.submit 'Save' %>
        </div>
    <% end %>
    

    then in your controller method should be as simple as this

    def create
        params[:task][:status_id] = 2
        @task = Task.new(params[:task])
        if @task.save!
           what you want to do if valid
        else
           go back to same page with errors
        end
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Project model which accepts nested attributes for Task. class Project <
I have a model with nested attributes : class Foo < ActiveRecord::Base has_many :bar
I have a Project model which accepts nested attributes for tasks. And Task has
I have a 3 models class Task < ActiveRecord::Base belongs_to :user, :dependent => :destroy
I have nested model as following Project model has_many keywords and keyword belongs_to project
I have a bus model, which has a nested route model. class Busables.Models.Bus extends
I have the following code in my models: Class Farm < ActiveRecord::Base has_many :farm_products,
I have a project model that has_many :tasks . I added a nested resource
I have a model, ModelRun , that accepts nested attributes for another model, ParameterValue
I have a Project having many Tasks, and each Tasks belongs to a Person.

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.