When to use @ and when to use self

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

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

On 11/21/06, Frederick C. [email protected] 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

On 11/21/06, Frederick C. [email protected] wrote:

again, thanks

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

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