Running through results of find()


#1

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.


#2

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://read.saush.com
http://jaccal.sourceforge.net


#3

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.


#4

My bad. That’s what happens when a noob tries to help another noob :frowning:
Thanks for clarifying.


#5

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


#6

No problem, just trying to be helpful.

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

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.


#7

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.