# Putting a calculation method in the model

**URL:** <https://www.ruby-forum.com/t/putting-a-calculation-method-in-the-model/128329>\
**Category:** Rails\
**Created:** [February 6, 2008, 8:01pm UTC](https://www.ruby-forum.com/t/putting-a-calculation-method-in-the-model/128329 "2008-02-06T20:01:16Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thufir](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/t/ee7513/32.png) [@Thufir](https://www.ruby-forum.com/u/Thufir)\
**Post date:** [February 6, 2008, 8:01pm UTC](https://www.ruby-forum.com/t/putting-a-calculation-method-in-the-model/128329/1 "2008-02-06T20:01:16Z")

</div>

Do I need to define @duration somewhere? I want to calculate the  
duration of a call in the model and then for the view to display that  
calculation.

Right now, the view, show.rhtml for calls, is giving an “You have a nil  
object when you didn’t expect it!” error ☹

thufir@arrakis ~/goodfellow-tool $  
thufir@arrakis ~/goodfellow-tool $ cat app/views/calls/show.rhtml -n |  
head -n 9 | tail -n 1  
9

The call duration was \<%= @call.duration.to\_s %\> long.

  
thufir@arrakis ~/goodfellow-tool $  
thufir@arrakis ~/goodfellow-tool $ cat app/models/call.rb  
class Call \< ActiveRecord::Base

def duration  
return (@end - @start).to\_time  
end

end  
thufir@arrakis ~/goodfellow-tool $

the trunk where the code is at:  
[http://code.google.com/p/goodfellow-tool/source/browse](http://code.google.com/p/goodfellow-tool/source/browse)

thanks,

Thufir
