Hi!
I’m at my wits end with this problem and hopefully someone can help
me.
I have code to calculate age based on date of death - date of birth.
I want the age to calculate down to the minute, which it is. However,
I have to account for all age units: minutes, hours, days, months, or
years. My problem is that I need to have if statements set up for
every scenario, but I want to exit the model when the correct age unit
has been established. Rails is continuting to evaluate the other if
statements, even when I’m checking for nil, and ends up returning 0 in
the case of an age unit less than a year. I have tried assembling the
if statements in a number of ways, including using a “return”
statement when the correct age unit has been calculated. This works,
but the model returns an error if there is more than one return
statement in existence. I just don’t know what else to try!! Any
help would be GREATLY appreciated!!
Here’s my code:
def age
dod = self.precase.report.dod
@age ||= begin
a = read_attribute(:age)
dateofbirth = read_attribute(:dob)
if not dateofbirth.nil?
if not dod.nil?
seconds = dod.to_time - dateofbirth.to_time
minutes = seconds / 60
m = minutes.to_i
if m.between?(1, 59)
m
end
hours = minutes / 60
h = hours.to_i
if not h.nil?
if h.between?(1, 23)
h
end
end
days = h / 24
d = days.to_i
if not d.nil?
if d.between?(1, 30)
d
end
end
months = days / 30
mths = months.to_i
if not mths.nil?
if mths.between?(1, 11)
mths
else
years = dod.year - dob.year-(dob.to_time.change(:year
=> dod.year) > dod ? 1 : 0)
end
end
end
end
end
end