[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

How to cycle through an instance

Tom Ha

12/16/2008 8:39:00 PM

Hi there,

I'm sorry for this noob question (but at least it must be a quick one
for you guys):

I have an instance of an object in my database and need to get out every
element of it, like this:

Example: model = student
========================

firstname = Fred
lastname = Williams
streetname = NULL
city = NULL
date of birth = 12.12.1934
...
(until there is no more column name left for this model)

How do I correctly/efficiently cycle through this one?

Thank you very much for your help!
Tom
--
Posted via http://www.ruby-....

4 Answers

Brian Candler

12/16/2008 9:23:00 PM

0

Tom Ha wrote:
> I have an instance of an object in my database and need to get out every
> element of it, like this:
>
> Example: model = student
> ========================
>
> firstname = Fred
> lastname = Williams
> streetname = NULL
> city = NULL
> date of birth = 12.12.1934
> ...
> (until there is no more column name left for this model)
>
> How do I correctly/efficiently cycle through this one?

Depends on what ORM you are using.

If it's ActiveRecord, try obj.attributes

(ActiveRecord questions probably best asked on the Rails mailing list
though)
--
Posted via http://www.ruby-....

Tom Ha

12/16/2008 9:29:00 PM

0

Thanks a lot !
--
Posted via http://www.ruby-....

Dejan Dimic

12/16/2008 9:59:00 PM

0

Have you try something like this:

@student = Student.find(params[:id])

@student.attribute_names.each do |attr_name|
puts "#{attr_name} => #{@student.attribute[attr_name]}\n"
end

or

puts @student.attribute_names.map { |attr_name| "#{attr_name} => #
{@student.attribute[attr_name]}"}.join("\n")


On Dec 16, 9:39 pm, Tom Ha <tom...@gmx.net> wrote:
> Hi there,
>
> I'm sorry for this noob question (but at least it must be a quick one
> for you guys):
>
> I have an instance of an object in my database and need to get out every
> element of it, like this:
>
> Example: model = student
> ========================
>
> firstname = Fred
> lastname = Williams
> streetname = NULL
> city = NULL
> date of birth = 12.12.1934
> ..
> (until there is no more column name left for this model)
>
> How do I correctly/efficiently cycle through this one?
>
> Thank you very much for your help!
> Tom
> --
> Posted viahttp://www.ruby-....

Tom Ha

12/17/2008 9:20:00 AM

0

Cool, thanks a bunch!
--
Posted via http://www.ruby-....