# When to use @ and when to use self

**URL:** <https://www.ruby-forum.com/t/when-to-use-and-when-to-use-self/76839>\
**Category:** Rails\
**Created:** [November 21, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/when-to-use-and-when-to-use-self/76839 "2006-11-21T17:45:56Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 21, 2006, 5:45pm UTC](https://www.ruby-forum.com/t/when-to-use-and-when-to-use-self/76839/1 "2006-11-21T17:45:56Z")

</div>

hey there,  
i am seeking some general info about how to get at a certain value and  
how to get that value in a method.

for example, lets say i have a machine.  
each machine has\_many :sensors  
so sensor belongs\_to :machine  
sensor also has\_many :reports  
each report has a duration field that is then number of seconds since  
the last report of a certain value  
now if i wanted to add the seconds together for a total when in a  
certain status, i would do something like this.  
total = 0  
for row in results  
duration = row[0]  
value = row[1]  
if value == ‘on’:  
total += duration

of course in rails we have sensor.value, sensor.duration  
so, anyway. i want to list in a table these figures.

so i need a method in sensor.rb to do this, right

so if i have for machine in machines do  
\<%= machine.sensor.total %\>

how do i declare that ?  
in the Sensor class, do i have  
def total  
i dont know what to put here to get the right sensor

and is it def total, or is it def self.total ?

thanks

so for sensor in machine.sensors |sensor| do

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 21, 2006, 8:02pm UTC](https://www.ruby-forum.com/t/when-to-use-and-when-to-use-self/76839/2 "2006-11-21T20:02:21Z")

</div>

You want to do

def total  
…  
end

because you want (I assume) to get the total for that particular  
instance of Sensor.  
You would then want to do something like

def total  
reports.sum ‘duration’  
end

Fred

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 21, 2006, 8:18pm UTC](https://www.ruby-forum.com/t/when-to-use-and-when-to-use-self/76839/3 "2006-11-21T20:18:41Z")

</div>

On 11/21/06, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> You would then want to do something like
> 
> > 
> 
> Hey thanks a lot. I appreciate it much.  
> i have not seen the duration.sum method before, i assume it adds all the  
> integers in that db field for the reports table ? thats cool too, i cant  
> use  
> it for this, because i can only add the ones where the status ==  
> ‘running’  
> but still , good thing for my notes.

again, thanks

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 21, 2006, 9:54pm UTC](https://www.ruby-forum.com/t/when-to-use-and-when-to-use-self/76839/4 "2006-11-21T21:54:58Z")

</div>

On 11/21/06, Frederick C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > again, thanks
> 
> > 
> 
> wow, i am so digging this. that line replaced 9 lines of php  
> this is really exciting, thanks  
> sk

---

<div class="post-metadata">

**Author:** ![nephish](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/8e7dd6/32.png) [@nephish](https://www.ruby-forum.com/u/nephish)\
**Post date:** [November 21, 2006, 9:11pm UTC](https://www.ruby-forum.com/t/when-to-use-and-when-to-use-self/76839/5 "2006-11-21T21:11:38Z")

</div>

shawn bright wrote:

> i have not seen the duration.sum method before, i assume it adds all the  
> integers in that db field for the reports table ? thats cool too, i cant  
> use  
> it for this, because i can only add the ones where the status ==  
> ‘running’  
> but still , good thing for my notes.
> 
> again, thanks

Sure you can: reports.sum :duration, :conditions =\> [‘status = ?’]

It’s basically the same as Report.sum, but scoped to reports that belong  
to that object. You can also do joins, group by etc… in the usual way.

Fred
