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 received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 11/21/06, Frederick Cheung <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > 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 > > -- > Posted via http://www.ruby-forum.com/. > > > > 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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, thanksSure 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 -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
On 11/21/06, Frederick Cheung <rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > > 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 > > -- > Posted via http://www.ruby-forum.com/. > > > > wow, i am so digging this. that line replaced 9 lines of phpthis is really exciting, thanks sk --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---