If anyone can help with this problem, I would greatly appreciate. In general I am trying to write a new method in one of models (Appointments) that takes a hash of objects and returns formated times that are within the objects and returns these times in an array. Without further ado, here is my current code: in Controller: def select @appt_obj = Appointment.find(:all) @newArray = Appointment.returnTime(@appt_obj) end in Appointments model: def self.returnTime(array) times = [] @app_obj = array @app_obj.each do |s| hold = s.start_time times << hold end return times end Then in my view I would like to iterate through the @newArray and display the times. I know this is way off, but I think it shows the general logic I am trying to accomplish. I have no experience writing functions in the model, so any advice on how to go about doing this is great. 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 -~----------~----~----~----~------~----~------~--~---
I may be missing something here, but wouldn''t it be much simpler to just do something like this: Controller #Get all the appointments def select @appt_obj = Appointment.find(:all) end View ... #Display all the start times in the view @appt_obj.each do |apt| %> Time <%= apt.start_time %> <% end ... Your class function (what you are doing in the model) looks like it should work. I wouldn''t set @app_obj and just use ''array.each do'' instead since @ designates an instance variable and you are using a class function, this may not matter, but if it isn''t working, that''s the only thing I see wrong with it. Hope I could be of some help. On Jul 23, 1:35 am, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> If anyone can help with this problem, I would greatly appreciate. In > general I am trying to write a new method in one of models > (Appointments) that takes a hash of objects and returns formated times > that are within the objects and returns these times in an array. > Without further ado, here is my current code: > > in Controller: > def select > @appt_obj = Appointment.find(:all) > @newArray = Appointment.returnTime(@appt_obj) > end > > in Appointments model: > def self.returnTime(array) > times = [] > @app_obj = array > @app_obj.each do |s| > hold = s.start_time > times << hold > end > return times > end > > Then in my view I would like to iterate through the @newArray and > display the times. I know this is way off, but I think it shows the > general logic I am trying to accomplish. I have no experience writing > functions in the model, so any advice on how to go about doing this is > great. 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Great. This has all been a great help. I will look into purchasing that book. On Jul 23, 5:45 am, AndyV <AndyVana...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I''d strongly recommend you pick up "Ruby for Rails" by David Black. > You seem to have a decent grasp of what Rails can do, but need some > help with the Ruby language itself. Black''s book is one of the most > helpful I''ve found. > > In this scenario you''d really benefit from using collect/map (one > aliases the other). The method iterates over a collection and allows > you to build a derivative array based on it''s contents. In this > case... > > @appointments > Appointment.find(:all, :order=>:start_time, :conditions=>"start_time> ?", Time.now) > > @appt_times = @appointments.collect{|appointment| > appointment.start_time} > > (The map implementation allows some shortcuts so the last line can be > shortened to @appt_times = @appointments.map(&:start_time)) > > In this case there is not really any business logic in the returnTime > method, so you could probably do all of that in the controller. > > On Jul 23, 1:35 am, Dave Lynam <dly...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > If anyone can help with this problem, I would greatly appreciate. In > > general I am trying to write a new method in one of models > > (Appointments) that takes a hash of objects and returns formated times > > that are within the objects and returns these times in an array. > > Without further ado, here is my current code: > > > in Controller: > > def select > > @appt_obj = Appointment.find(:all) > > @newArray = Appointment.returnTime(@appt_obj) > > end > > > in Appointments model: > > def self.returnTime(array) > > times = [] > > @app_obj = array > > @app_obj.each do |s| > > hold = s.start_time > > times << hold > > end > > return times > > end > > > Then in my view I would like to iterate through the @newArray and > > display the times. I know this is way off, but I think it shows the > > general logic I am trying to accomplish. I have no experience writing > > functions in the model, so any advice on how to go about doing this is > > great. 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@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---