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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T07:17:45+00:00 2026-06-17T07:17:45+00:00

Getting started with Rails and I’m stuck with a block operation. I want to

  • 0

Getting started with Rails and I’m stuck with a block operation. I want to loop through a model and update integer values for a new field by calculating difference between two datetime fields. Doing this in the console:

MyModel.all.each do |m|
  m.new_integer_field = m.existing_datetime_field - m.parent_object.existing_datetime_field
  m.save!
end

The result is

 NoMethodError: undefined method `-' for nil:NilClass

It works on one record if I do:

m = MyModel.find(1)
m.new_integer_field = m.existing_datetime_field - m.parent_object.existing_datetime_field
m.save

I guess it’s a basic syntax thing, but couldn’t find obvious explanations. Grateful to get some pointers forward.

  • 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-17T07:17:46+00:00Added an answer on June 17, 2026 at 7:17 am

    The m.existing_datetime_field in one of the instances was nil and therefore the operation didn’t work. You need to check for nil values if not sure:

    MyModel.all.each do |m|
      unless m.existing_datetime_field == nil
        m.new_integer_field = m.existing_datetime_field - m.parent_object.existing_datetime_field
      m.save!
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

After following the RoR getting started tutorial, I added another model as: $ rails
I'm new to Rails, and am following the getting started guide on their website.
Getting started with TDD and I want to ground up a Repository-driven Model. However,
I'm new to Ruby on Rails, and I just finished following the getting started
I'm getting started with rails and devise for authentication and I want to make
I've gone through the getting started with rails tutorial and now i'm trying to
I'm just getting started with rails and I'm a little confused reading through different
So I'm getting started learning Rails. Now that Rails 3 is out, I want
I'm going through the Getting Started with Rails guide and got confused with section
I'm just getting started with rails gems, and wondering how best to add functionality

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.