On Dec 16, 2008, at 4:40 PM, Frederick Cheung wrote:
> Iterate over some_model_instance.attributes ?
>
> Sent from my iPhone
>
> On 16 Dec 2008, at 21:17, Tom Ha <[email protected]>
> wrote:
>
>>
>> Hi there,
>>
>> 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)
>>
>> If it was an array, I'd use: .each { ... }
>>
>> How do I correctly/efficiently cycle through this one?
>>
>> Thank you very much for your help!
>> Tom
Or Student.column_names.each {|column| puts "#{column} =
#{student.send(column)}" }
You might display something like column.humanize.downcase if you need
'date of birth' with the spaces.
-Rob
Rob Biedenharn http://agileconsultingllc.com
[email protected]
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups "Ruby
on Rails: Talk" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---