# Writing a new function in the model problem

**URL:** <https://www.ruby-forum.com/t/writing-a-new-function-in-the-model-problem/143844>\
**Category:** Rails\
**Created:** [July 23, 2008, 7:36am UTC](https://www.ruby-forum.com/t/writing-a-new-function-in-the-model-problem/143844 "2008-07-23T07:36:02Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [July 23, 2008, 7:36am UTC](https://www.ruby-forum.com/t/writing-a-new-function-in-the-model-problem/143844/1 "2008-07-23T07:36:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [July 23, 2008, 10:45am UTC](https://www.ruby-forum.com/t/writing-a-new-function-in-the-model-problem/143844/2 "2008-07-23T10:45:36Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [July 23, 2008, 2:46pm UTC](https://www.ruby-forum.com/t/writing-a-new-function-in-the-model-problem/143844/3 "2008-07-23T14:46:26Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![paradise](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/8e7dd6/32.png) [@paradise](https://www.ruby-forum.com/u/paradise)\
**Post date:** [July 23, 2008, 9:04pm UTC](https://www.ruby-forum.com/t/writing-a-new-function-in-the-model-problem/143844/4 "2008-07-23T21:04:36Z")

</div>

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