Reputation: 93
I have this hash.
[#<PartnerSite id: 20, site_name: "test123", user_id: 178,
market_id: 164, created_at: "2014-01-15 18:02:01",
updated_at: "2014-01-15 18:02:01",
ip_address_range: "50.19.93.173",
public_key: "453df9eeeb4a2d85bb7ffeb85486d489">]
And I am trying to access value by using
partner_site[public_key]
partner_site.public_key
But it's showing error like
undefined local variable or method 'public_key'
Andundefined method `public_key'
Respectively.Upvotes: 0
Views: 122
Reputation: 48318
First of all, that looks like an array containing one PartnerSite
object, not a hash. So you'll want to either get the first object in the array with first
, (E.g. partner_site = partner_site.first
) or loop through all the elements in the array with each
and do something to all of them.
Secondly, when using the []
form for accessing values you usually need to provide either a symbol or a string to the []
method. In partner_site[public_key]
, public_key
references an undefined variable. Instead, you should do partner_site[:public_key]
or partner_site["public_key"]
.
Your other form, partner_site.public_key
doesn't work because either you're calling public_key
on the Array and not the PartnerSite
object (in which case see the first paragraph in this answer), or because PartnerSite
doesn't have a #public_key
method defined on it. In the second case, you can probably fix that by adding attr_accessor :public_key
to the PartnerSite
class.
Upvotes: 2