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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T02:07:42+00:00 2026-05-27T02:07:42+00:00

I wrote an instance method in my model for calculating a time difference between

  • 0

I wrote an instance method in my model for calculating a time difference between two dates. Works when the second date exists, but need to add an if else condition to return a string if the second date does not exist. I have

class Incident  
def calculate_elapsed(difference)
if self.closed.nil?
  @difference = "Open"
else
  d1 = self.created
  d2 = self.closed
  difference = d2 - d1
  @difference = difference.to_i
end 

When I try to load an Incident which I know to have an empty closed date I get

You have a nil object when you didn't expect it!

You might have expected an instance of Array.

The error occurred while evaluating nil.-

Puzzled by the error because I figured if self.closed.nil?
was checking for the nil object. What can I do to get this to work?

I’m not certain this is the right way to handle what I’m trying to do, so open to any and all suggestions (i.e. this should be a class method, helper, etc. Had it in my controller previously and figured this was a step in the right direction).

What’s further confusing is that I can call this method in a partial and it returns the “Open” string wherever the closed date does not exist.

Edit

Here’s what I got working-

Model:

class Incident  
def calculate_elapsed
if self.closed.blank?
  @difference = "Complaint Open"
else
  d1 = created
  d2 = closed
  difference = d2 - d1
  difference_clean = difference.to_i
  @difference = difference_clean.to_s + ' Days'
end 
end

show.html.erb

<%= @incident.calculate_elapsed %>

_incidents.html.erb partial

<%= incident.calculate_elapsed %>
  • 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-27T02:07:43+00:00Added an answer on May 27, 2026 at 2:07 am

    The error should be somewhere else, because nil? method is defined on Objects and nil is also an object.

    As my last comment probably helped, possibly your class definition in you Rails environment was outdated, so restarting the application forced Rails to reload the source.

    You can also reload the classes with reload! method for e.g. in Rails console.

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

Sidebar

Related Questions

I wrote something like this: instance Functor (Either e) where fmap _ (Left a)
I wrote an application that currently runs against a local instance of MySql. I
I have a model class Project and for each model instance, there should be
Hi i wrote a method to calculate the number of times a number appears
I wrote these lines but i have NullReferenceException. please help me how to fix
I am trying to write a generic method for deserializing json into my model.
I have a problem using a method defined on a model in CanCan abilities:
Currently, I do serialize my model object to the SharedObject instance : try {
I'd like to write a generic method that creates a new instances of a
For instance is it possible to write this if (variable != null) without using

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.