writing a new function in the model problem

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.

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.

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.

Great. This has all been a great help. I will look into purchasing that book.