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.
48b67c246071276990d6cb584b34c95a?d=identicon&s=25 Steven De ryck (-redyck-)
on 2006-04-14 16: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.
D2a5b7975f49e051c1de10f98ea81f63?d=identicon&s=25 Chang Sau Sheong (Guest)
on 2006-04-14 16: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 Sheong

http://blog.saush.com
http://read.saush.com
http://jaccal.sourceforge.net
58c6efb8466b9f85155fe6aa9fc37fce?d=identicon&s=25 Chris T (Guest)
on 2006-04-14 20: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
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-04-14 23: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 Mornini
58c6efb8466b9f85155fe6aa9fc37fce?d=identicon&s=25 Chris T (Guest)
on 2006-04-15 00:31
(Received via mailing list)
My bad. That's what happens when a noob tries to help another noob :-(
Thanks for clarifying.
59de94a56fd2c198f33d9515d1c05961?d=identicon&s=25 Tom Mornini (Guest)
on 2006-04-15 01: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 Mornini
58c6efb8466b9f85155fe6aa9fc37fce?d=identicon&s=25 Chris T (Guest)
on 2006-04-15 09: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.