rails how to display data or retrieve values after find -
i did find on model , got following
>> @addy => [#<address id: 3, street: "some street", housenumber: nil, created_at: "2010-01-20 06:09:52", updated_at: "2010-01-20 06:09:52", address_id: 16>]
now how retrieve values? if want street?
@addy.street
not work netiher @addy[:street]
what if had list of @addy's , wanted loop through them show street each?
@addy.first.street
should work, list of adress classes containing 1 member.
for displaying each street more adresses in list:
@addy.each |address| puts address.street end
or
list_of_streets = @addy.map { |address| address.street }
edit:
when have problem identifying class have, check object.class
. when use object
in irb, see output object.inspect
, doesn't have show class name or can spoof informations. can call object.methods
, object.public_methods
.
and remember in ruby class instance (of class class). can call @addr.methods
instance methods , @addr.class.methods
class methods.
edit2:
in rails #find
can take first parameter symbol, , if pass :first
of :last
, you'll 1 object. same if pass 1 integer (id of retrieved object). in other cases list result, if contains 1 object.
Comments
Post a Comment