I have a Hash and need to use the value year from a variable in it The example is:
2.2.4 :001 > @values_project
=> [#<CustomValue id: 20367, customized_type: "Project", customized_id: 492, custom_field_id: 64, year: "2017">]
and I need the year
so when I try @value_hash.year
receive the error
NoMethodError: undefined method `year' for
#<ActiveRecord::Relation:0x0000000712fb18>
I don't understand why
@value_hash
is not ahash
, it's anActiveRecord::Relation
(as the error states).In your example,
@value_hash
has only one member. To get that member, which is an instance of the classCustomValue
(which is still not ahash
!), you can do:Then, to get the year, you can do:
Or, you could do it in one shot as:
Which is just a long way of saying exactly what Sachin R said (so you should accept their answer).