Rails, Finding objects in complex hashes

201 views Asked by At

I'm building an app that integrates with Highrise, and so-far-so-good, however, when trying to import a person Highrise::People it spits out a huge hash similar to this:

[ ...,  #<Highrise::Person:0x1035084b8 @attributes={"contact_data"=>#<Highrise::Person::ContactData:0x1034f8b30 @attributes={"email_addresses"=>[], "addresses"=>[], "web_addresses"=>[], "phone_numbers"=>[], "twitter_accounts"=>[], "instant_messengers"=>[]}, @prefix_options={}>, "created_at"=>Sat Nov 28 05:38:26 UTC 2009, "title"=>"president", "updated_at"=>Sat Nov 28 05:38:27 UTC 2009, "background"=>"asdfasdfadsfas", "id"=>27569370, "owner_id"=>nil, "group_id"=>nil, "company_id"=>27569371, "last_name"=>"Doe", "author_id"=>192208, "visible_to"=>"Everyone", "first_name"=>"John"}, @prefix_options={}>, ... ]

It looks like that right inside of each Highrise::Person is an @attributes which is another Highrise::Person::ContactData, which itself is another @attributes with arrays like email_addresses[] and phone_numbers[] as well as simple key/values...

Sorry to be so confusing, what I want to know is how would I just get the first_name of each person from a hash like this?

Probably super simple I'm just confused...

Update

I guess a better way to phrase it would be, given the hash above, why won't this work:

@people = Highrise::Person.find(:all)

for person in @people do
   person.attributes["first_name"]
end
2

There are 2 answers

0
Steinbitglis On BEST ANSWER

If yourlist is an array of Persons

yourlist.each do |person|
    puts person.attributes["first_name"]
end

If Person has an attr_reader, that is.

0
jrich On

You can also do:

@people.each do |person|
    puts person.first_name
end