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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T02:05:43+00:00 2026-06-15T02:05:43+00:00

I have a Client model which has many projects. In the project model I

  • 0

I have a Client model which has many projects. In the project model I want to validate that the project start date is always before or on the same day as the project end date. This is my project model:

class Project < ActiveRecord::Base
  attr_accessible :end_on, :start_on, :title

  validates_presence_of :client_id, :end_on, :start_on, :title
  validate :start_has_to_be_before_end

  belongs_to :clients

  def start_has_to_be_before_end
    if start_on > end_on
        errors[:start_on] << " must not be after end date."
        errors[:end_on] << " must not be before start date."
    end
  end
end

My application works as expected and gives me the specified errors in case the validation fails.

However, in my unit test for the projects, I am trying to cover this scenario, deliberately setting the start date after the end date:

test "project must have a start date thats either on the same day or before the end date" do
    project = Project.new(client_id: 1, start_on: "2012-01-02", end_on: "2012-01-01", title: "Project title")
    assert !project.save, "Project could be saved although its start date was after its end date"
    assert !project.errors[:start_on].empty?
    assert !project.errors[:end_on].empty?
end

Strangely, running this test gives me three errors, all referring to this line if start_on > end_on in my validation method, saying undefined method '>' for nil:NilClass twice and comparison of Date with nil failed once.

What can I do to make the tests pass?

  • 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-15T02:05:45+00:00Added an answer on June 15, 2026 at 2:05 am

    You are creating a Project that has string values for :start_on and :end_on. That’s unlikely to work. Rails might try to be smart and parse those, I’m not sure.. I wouldn’t count on it. Odds are some coercion is going on and the values are getting set to nil.

    I would do this:

    project = Project.new(client_id: 1, 
                          start_on: 2.days.from_now.to_date, 
                          end_on: Time.now.to_date, 
                          title: "Project title")
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a model Client which has many :tours, association in it. I am
I have a model like this: Client - which has many Locations - which
I have a business model called Customer which has many required properties (via DataAnnotations)
Hello I have a model named Client which has nested models called Receiver and
I have an Appointment model, each instance of which has a Client . Here's
I have HABTM models Client and Book . Client model has a bookshelf_color attribute
I currently have a client that wants me to 'abstract' out a domain model
In a client application I have a DBIx::Class model 'Todo' that can be linked
I have a model with many fields (nearly 40). The client wants the fields
I have a django application which has one main table/model which references various 'lookup'

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.