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

  • Home
  • SEARCH
  • 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 3424368
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T06:27:58+00:00 2026-05-18T06:27:58+00:00

I have a Reservation model that takes an appointment attribute as date and has

  • 0

I have a Reservation model that takes an appointment attribute as date and has a virtual attribute duration that indicates how long the appointment will take. The real attribute, booking_end takes a Time that is referenced all over my application. However, for ease of input, we use duration instead of choosing another Time. The fields are below:

def duration
  ( (booking_end - date) / 1.hour ).round( 2 ) rescue nil
end

def duration=(temp)
  if ( true if Float(temp) rescue false )
    self.booking_end = time_round(date + temp.to_f.hours, 15.minutes)
  else  
    errors.add(:duration, "must be a number, stated in hours.")
    self.booking_end = nil
  end
end

The whole thing fails when I reference the date field while creating a new record. I get a ‘nil’ error because date hasn’t been initialized. How can I fix this problem? The rest of this works when updating existing records.

  • 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-18T06:27:58+00:00Added an answer on May 18, 2026 at 6:27 am

    When you call Reservation.new(:date => date, :duration => duration) ActiveRecord::Base assigns attributes values this way (see assign_attributes method):

    attributes.each do |k, v|
      ...
      respond_to?("#{k}=") ? send("#{k}="", v) 
      ...
    

    Hash#each method iterates through the values the way that :duration key is accessed before :date one, so date is nil inside the duration= method:

    ruby-1.8.7-p302 > {:date => Date.today, :duration => 5}.each do |key,value|
    ruby-1.8.7-p302 >     puts "#{key} = #{value}"
    ruby-1.8.7-p302 ?>  end
    duration = 5
    date = 2010-11-17
    

    So you’ll have to call duration= after initialization.

    Or you can redefine Reservation#initialize to call super with :date and then update_attributes with the rest of parameters.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a Reservation model that I'm searching for with three fields. The container_id
I have a model that has an amount and I'm tracking to see if
So I have a method in a reservation model called add_equip. This method does
I have 2 table Reservation Guest Reservation has a FK to Guest (GuestId) When
I have a Web Service written in .NET that provides data for an iPhone
I am trying to learn Microsoft MVC 2, and have in that case found
As my Rails app matures, it's becoming increasingly apparent that it has a strong
to KIS I have two models: Reservations and Containers . Container has_many :reservations Reservation
In my application i have three tables, reservation , patient and sub_unit , i
Using NHibernate I need to insert an entity into a database that has a

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.