Rails、複雑なハッシュ内のオブジェクトの検索
-
06-07-2019 - |
質問
私はHighriseと統合するアプリを構築していますが、これまでのところ非常に良いのですが、 Highrise :: People
をインポートしようとすると、これ:
[ ..., #<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={}>, ... ]
各 Highrise :: Person
のすぐ内側が @attributes
で、別の Highrise :: Person :: ContactData
のように見えます、それ自体は別の @attributes
で、 email_addresses []
や phone_numbers []
などの配列と単純なキー/値...
非常に紛らわしいので、私が知りたいのは、このようなハッシュから各人の first_name
をどのように取得するかです。
恐らく非常にシンプルで、私はただ混乱しています...
更新
上記のハッシュを考えると、これがうまく機能しない理由は次のとおりです。
@people = Highrise::Person.find(:all)
for person in @people do
person.attributes["first_name"]
end
解決
yourlist がPersonsの配列である場合
yourlist.each do |person|
puts person.attributes["first_name"]
end
Person にattr_readerがある場合、つまり。
他のヒント
次のこともできます。
@people.each do |person|
puts person.first_name
end
所属していません StackOverflow