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
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.
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
This forum is not affiliated to the Ruby language, Ruby on Rails framework, nor any Ruby applications discussed here.