Accessing This Array In Ruby -
i'm playing around in console.
2.0.0-p451 :060 > u = user.find(1) user load (0.2ms) select "users".* "users" "users"."id" = ? limit 1 [["id", 1]] => #<user id: 1, name: "patrick sullivan", email: "xxxx@gmail.com", created_at: "2014-03-10 20:35:43", updated_at: "2014-03-25 00:48:45", password_digest: "$2a$10$zxeejtjaompdope.3h7avodo9xmcuajwakim10dwtz3i...", remember_token: "61ed84e1184181365e0591e6a309e575f6072b69", admin: true, image_file_name: nil, image_content_type: nil, image_file_size: nil, image_updated_at: nil, city_id: nil, hood_id: nil, burrough_id: nil> 2.0.0-p451 :061 > u.pending_invited_by user load (0.2ms) select "users".* "users" inner join "friendships" on "users"."id" = "friendships"."friendable_id" "friendships"."friend_id" = ? , (("friendships"."pending" = 't' , "friendships"."blocker_id" null)) [["friend_id", 1]] => #<activerecord::associations::collectionproxy [#<user id: 102, name: "patrick sullivan", email: "deedee@gmail.com", created_at: "2014-03-11 00:12:19", updated_at: "2014-03-25 00:48:20", password_digest: "$2a$10$y6t3ppmqkmomxio6c/5xe.hz4gypcuvy907ipypahpuk...", remember_token: "7c8d16661fa92747516970fbf3aafae6151c2848", admin: false, image_file_name: "aboutuspeendee.png", image_content_type: "image/png", image_file_size: 219210, image_updated_at: "2014-03-14 15:32:47", city_id: "brooklyn", hood_id: nil, burrough_id: nil>]> what have append u.pending_invited_by have name of user 102 returned (and i'm aware both patrick sullivan).
this should easy upvote , green someone. i've been playing in console 15 minutes. i'm new ruby , rails!
thanks guys!
you can use map return result of calling method on each member of activerecord::collectionproxy (it's not array, acts one).
because want call name on each member of collection, can call
.map { |u| u.name } or shorten
.map(&:name)
Comments
Post a Comment