0

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!

1
  • Bam. That did it. Sevens, if you'd like to post that I'd give you an upvote and green. I'll have to google some stuff. ie what .map does =) Commented Mar 25, 2014 at 1:35

2 Answers 2

2

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) 
Sign up to request clarification or add additional context in comments.

5 Comments

If I wanted to loop through the images too, could I do {|u| u.name, u.image}
what are you actually doing this for? If it's for output in a view, you can simply iterate over the collection with each, and then print out whatever you like for each user.
This worked. <%= current_user.pending_invited_by.map do |u| %> <%= image_tag u.image.url(:thumb) %> <%= u.name. %> <% end %>
anyway to get rid of that ["\n"]
You're using map, which will return the result of the block, and then you're printing it out. You don't need the = in the <%= current_user part. You also don't need map here - a simple each will suffice.
2

If you want the raw values and not instances of User, use #pluck

u.pending_invited_by.pluck(:name) # ['Patrick Sullivan'] u.pending_invited_by.pluck(:name).first # 'Patrick Sullivan' 

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.