Forum: Ruby on Rails Running through results of find()

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Steven D. (Guest)
on 2006-04-14 18:33
Hi,

I've got a question about the find() method in RoR

This is my code:
actions=Action.find(:all, :conditions => "ActivityID='actid'")

This should return a list, array, hash, ... ? of Actions.
What type of data does find() return? And how do I run through it?

I would like to show Action.ActionCode for every action in actions.

I tried:
for action in actions
 output += action.ActionCode
end

Didn't work.

Thanks,

Steven.
Chang Sau S. (Guest)
on 2006-04-14 18:37
(Received via mailing list)
Um, this seems to be a question of understanding Ruby the programming
language rather than understanding Rails the framework.

#find returns an Array object. There are many ways to iterate through an
array, you should check the Ruby documentation here:
http://www.ruby-doc.org/core/

Steven De Ryck wrote:
> I would like to show Action.ActionCode for every action in actions.
> Steven.
>
>


--
Sau S.

http://blog.saush.com
http://read.saush.com
http://jaccal.sourceforge.net
Chris T (Guest)
on 2006-04-14 22:55
(Received via mailing list)
It returns an array of hashes. Try going into the console (ruby
script/console) and doing your

actions=Action.find(:all...)

and it'll echo the resulting array.

By the way, action.ActionCode doesn't look right -- attributes are
normally lower case.

Hope this helps
Tom M. (Guest)
on 2006-04-15 01:18
(Received via mailing list)
On Apr 14, 2006, at 11:51 AM, Chris T wrote:

> It returns an array of hashes. Try going into the console (ruby
> script/console) and doing your

No, it'll return an array of Action objects. Assuming Action is an
ActiveRecord subclass, it's
definitely NOT a hash.

--
-- Tom M.
Chris T (Guest)
on 2006-04-15 02:31
(Received via mailing list)
My bad. That's what happens when a noob tries to help another noob :-(
Thanks for clarifying.
Tom M. (Guest)
on 2006-04-15 03:14
(Received via mailing list)
No problem, just trying to be helpful.

Let me guess: You (like I) came from Perl? :-)

There are *no* built in types in Ruby, everything
is an object. The question is, what type of object
is it?

I only point this out because I see a lot of new
users suggesting that most output from .inspect
is displaying hashes, when they're really looking
at objects that are NOT hashes.

I've learned a lot by helping others on the list,
and occasionally being wrong and getting corrected,
so keep up the good work!

-- -- Tom M.
Chris T (Guest)
on 2006-04-15 11:33
(Received via mailing list)
No, PHP. About 25% through the Pickaxe at the moment (and would
recommend to all noobs that they take the time to read it -- rails
starts to make much more sense). Taking a bit of time for the whole OO
thing to sink in, but actually I'm lovin it.
This topic is locked and can not be replied to.