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. -- Posted via http://www.ruby-forum.com/.
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:> 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. > >-- Sau Sheong http://blog.saush.com http://read.saush.com http://jaccal.sourceforge.net
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 Steven De Ryck wrote:> 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. > >
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 yourNo, it''ll return an array of Action objects. Assuming Action is an ActiveRecord subclass, it''s definitely NOT a hash. -- -- Tom Mornini
My bad. That''s what happens when a noob tries to help another noob :-( Thanks for clarifying. Tom Mornini wrote:> 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 > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
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 On Apr 14, 2006, at 3:28 PM, Chris T wrote:> My bad. That''s what happens when a noob tries to help another > noob :-( Thanks for clarifying. > > Tom Mornini wrote: >> 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 >> _______________________________________________ >> Rails mailing list >> Rails@lists.rubyonrails.org >> http://lists.rubyonrails.org/mailman/listinfo/rails
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. Tom Mornini wrote:> 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 > > On Apr 14, 2006, at 3:28 PM, Chris T wrote: > >> My bad. That''s what happens when a noob tries to help another noob >> :-( Thanks for clarifying. >> >> Tom Mornini wrote: >>> 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 >>> _______________________________________________ >>> Rails mailing list >>> Rails@lists.rubyonrails.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails > > _______________________________________________ > Rails mailing list > Rails@lists.rubyonrails.org > http://lists.rubyonrails.org/mailman/listinfo/rails >