Hey there, i have an app with a machines table. each machine has_many :status_changes now, what i need to do is pull the most recent value in a column called previous in the status_changes table for a particular machine. what i am confused about is how and where to do this. i would think in the machine model ? something like def previous_condition but i dont know where to put what will pull the most recent record for the status_changes for the machine model. in sql it would be something along the lines of select `previous` from `status_changes` where `machine_id` = id_number order by date_time desc. sorry if this seems pretty simple, i am still kinda new here in rails. 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 -~----------~----~----~----~------~----~------~--~---
if machine is an instance of Machine, then machine.status_changes.find :first, :order => ''date_time desc'' will get you the most recent status_change for that machine 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 -~----------~----~----~----~------~----~------~--~---
Hi Nephish In my app, I have a customers table that has_many devices that have been returned for repair. These are called rmas. So a customer has_many rmas and an rma belongs_to customer. I put methods like find_rmas_for_customer(name) either in the customers_controller.rb, if I''m only calling it in one or two customer actions, or in the application.rb where other controller actions can call it. It''s really up to you. So in your machine controller, you might define your action previous_condition where you pass the machine id as a parameter: def previous_condition @status = Status.find(:first, :from => ''machines, status_changes'', :conditions=> [''status_changes.machine_id ?'', params[:id]]) end Obviously, this is not tested. But hopefully, it gives you some ideas. Regards, Dave On Nov 17, 5:30 am, "nephish" <neph...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Hey there, > i have an app with a machines table. each machine has_many > :status_changes > > now, what i need to do is pull the most recent value in a column called > previous > in the status_changes table for a particular machine. > what i am confused about is how and where to do this. > > i would think in the machine model ? something like > def previous_condition > > but i dont know where to put what will pull the most recent record for > the status_changes for the machine model. > > in sql it would be something along the lines of > select `previous` from `status_changes` where `machine_id` = id_number > order by date_time desc. > > sorry if this seems pretty simple, i am still kinda new here in rails. > > 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 -~----------~----~----~----~------~----~------~--~---
Frederick, Archer, thanks, this is good. i have been concerned about what i can put in a controller, because some of this stuff will will have to be referenced from several different pages ( or views ). And the scale of it is a concern to me. for example, i have customers that have many machines, that have have many status changes. machines also have many stats, have many reports ( of status ) so i was concerned about how far down the dependency chain something can go before something breaks down. like customer.machine.sensor.report should i be concerned about this sort of thing? thanks for your help with these by the way. 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 -~----------~----~----~----~------~----~------~--~---
I''ve recently read a useful suggestion (apologies for not citing the actual author), that recommends using a view called shared and putting common pages there. I try to stay away from mixing views and controllers as much as possible, simply because I''ve found it quickly gets confusing as hell. (well, my code does, anyway) I''ve only gone down three levels of has_many/belongs_to associations, but with the same niggling concern. Perhaps someone else with more experience can comment on how well this scales. You''re welcome to any small assistance I can offer. Dave On Nov 17, 6:21 am, "nephish" <neph...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Frederick, Archer, > thanks, > this is good. i have been concerned about what i can put in a > controller, because some of this stuff will will have to be referenced > from several different pages ( or views ). > And the scale of it is a concern to me. > > for example, i have customers that have many machines, that have have > many status changes. machines also have many stats, have many reports ( > of status ) > so i was concerned about how far down the dependency chain something > can go before something breaks down. > like customer.machine.sensor.report > > should i be concerned about this sort of thing? > > thanks for your help with these by the way. > 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 -~----------~----~----~----~------~----~------~--~---
On 11/20/06, Archer <dfdumaresq-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > > I''ve recently read a useful suggestion (apologies for not citing the > actual author), that recommends using a view called shared and putting > common pages there. I try to stay away from mixing views and > controllers as much as possible, simply because I''ve found it quickly > gets confusing as hell. (well, my code does, anyway) > > I''ve only gone down three levels of has_many/belongs_to associations, > but with the same niggling concern. Perhaps someone else with more > experience can comment on how well this scales. > > You''re welcome to any small assistance I can offer. > Dave > > On Nov 17, 6:21 am, "nephish" <neph...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > Frederick, Archer, > > thanks, > > this is good. i have been concerned about what i can put in a > > controller, because some of this stuff will will have to be referenced > > from several different pages ( or views ). > > And the scale of it is a concern to me. > > > > for example, i have customers that have many machines, that have have > > many status changes. machines also have many stats, have many reports ( > > of status ) > > so i was concerned about how far down the dependency chain something > > can go before something breaks down. > > like customer.machine.sensor.report > > > > should i be concerned about this sort of thing? > > > > thanks for your help with these by the way. > > sk > > > > > Thanks, we shall see how it goes ;)-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 -~----------~----~----~----~------~----~------~--~---
Archer wrote:> Hi Nephish > In my app, I have a customers table that has_many devices that have > been returned for repair. These are called rmas. > So a customer has_many rmas and an rma belongs_to customer. > > I put methods like find_rmas_for_customer(name) either in the > customers_controller.rb, if I''m only calling it in one or two customer > actions, or in the application.rb where other controller actions can > call it. > It''s really up to you. > > So in your machine controller, you might define your action > previous_condition where you pass the > machine id as a parameter: > > def previous_condition > @status = Status.find(:first, > :from => ''machines, status_changes'', > :conditions=> [''status_changes.machine_id > ?'', params[:id]]) > end > > Obviously, this is not tested. But hopefully, it gives you some ideas. > > Regards, > Dave > > On Nov 17, 5:30 am, "nephish" <neph...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: >> Hey there, >> i have an app with a machines table. each machine has_many >> :status_changes >> >> now, what i need to do is pull the most recent value in a column called >> previous >> in the status_changes table for a particular machine. >> what i am confused about is how and where to do this. >> >> i would think in the machine model ? something like >> def previous_condition >> >> but i dont know where to put what will pull the most recent record for >> the status_changes for the machine model. >> >> in sql it would be something along the lines of >> select `previous` from `status_changes` where `machine_id` = id_number >> order by date_time desc. >> >> sorry if this seems pretty simple, i am still kinda new here in rails.Sorry for the late reply - I''m catching up on a 2000+ message backlog! In the i2 wiki, this technique is used to link the latest version to a page: class Page < ActiveRecord::Base belongs_to :book has_many :versions, :order => "created_at", :dependent => true has_one :current_version, :class_name => "Version", :order => "created_at DESC" # ... end Sneaky! But it''s DHH''s code :-) regards Justin --~--~---------~--~----~------------~-------~--~----~ 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 12/2/06, Justin Forder <justin-zSfPWr5aQuznITO/+xaoB7VCufUGDwFn@public.gmane.org> wrote:> > > Archer wrote: > > Hi Nephish > > In my app, I have a customers table that has_many devices that have > > been returned for repair. These are called rmas. > > So a customer has_many rmas and an rma belongs_to customer. > > > > I put methods like find_rmas_for_customer(name) either in the > > customers_controller.rb, if I''m only calling it in one or two customer > > actions, or in the application.rb where other controller actions can > > call it. > > It''s really up to you. > > > > So in your machine controller, you might define your action > > previous_condition where you pass the > > machine id as a parameter: > > > > def previous_condition > > @status = Status.find(:first, > > :from => ''machines, status_changes'', > > :conditions=> [''status_changes.machine_id > > ?'', params[:id]]) > > end > > > > Obviously, this is not tested. But hopefully, it gives you some ideas. > > > > Regards, > > Dave > > > > On Nov 17, 5:30 am, "nephish" <neph...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > >> Hey there, > >> i have an app with a machines table. each machine has_many > >> :status_changes > >> > >> now, what i need to do is pull the most recent value in a column called > >> previous > >> in the status_changes table for a particular machine. > >> what i am confused about is how and where to do this. > >> > >> i would think in the machine model ? something like > >> def previous_condition > >> > >> but i dont know where to put what will pull the most recent record for > >> the status_changes for the machine model. > >> > >> in sql it would be something along the lines of > >> select `previous` from `status_changes` where `machine_id` = id_number > >> order by date_time desc. > >> > >> sorry if this seems pretty simple, i am still kinda new here in rails. > > Sorry for the late reply - I''m catching up on a 2000+ message backlog! > > In the i2 wiki, this technique is used to link the latest version to a > page: > > class Page < ActiveRecord::Base > > belongs_to :book > > has_many :versions, :order => "created_at", :dependent => true > > has_one :current_version, > :class_name => "Version", > :order => "created_at DESC" > # ... > end > > Sneaky! But it''s DHH''s code :-) > > regards > > JustinHey cool, thanks for this. I am doing something similar and its working. Thanks for taking your time on this. shawn> >--~--~---------~--~----~------------~-------~--~----~ 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 -~----------~----~----~----~------~----~------~--~---