Reputation: 1560
I'm playing around in the console.
2.0.0-p451 :060 > u = User.find(1)
User Load (0.2ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT 1 [["id", 1]]
=> #<User id: 1, name: "Patrick Sullivan", email: "[email protected]", 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".* FROM "users" INNER JOIN "friendships" ON "users"."id" = "friendships"."friendable_id" WHERE "friendships"."friend_id" = ? AND (("friendships"."pending" = 't' AND "friendships"."blocker_id" IS NULL)) [["friend_id", 1]]
=> #<ActiveRecord::Associations::CollectionProxy [#<User id: 102, name: "Patrick Sullivan", email: "[email protected]", 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 would I have to append to u.pending_invited_by to have the name of user 102 to be returned (and I'm aware they are both Patrick Sullivan).
This should be an easy upvote and green for someone. I've been playing in console for 15 minutes. I'm new to ruby and rails!
Thanks guys!
Upvotes: 0
Views: 67
Reputation: 25049
You can use map
to return the result of calling a method on each member of the ActiveRecord::CollectionProxy (it's not an array, but it acts like one).
Because you want to call name
on each member of the collection, you can call
.map { |u| u.name }
or simply shorten that to
.map(&:name)
Upvotes: 2