tzoukos
tzoukos

Reputation: 86

Show only username of user with ruby on rails

The title may not be very appropriate, so apologies in advance.

I'm building a rails app (not really experienced I'd say) and I used the Amistad gem in order to add friendhsips. It has some cool features, like showing all friends. Unfortunately, I can't make it work as I wish.

I'm trying to show all users with pending request to the current user with:

Pending by: <%= current_user.pending_invited_by %>

But what I get as a result is

Pending by: [#<User avatar_url: nil, id: 16, username: "zxczxc", 
email: "[email protected]", created_at: "2011-10-01 16:50:24", 
updated_at: "2011-10-05 00:48:24", 
encrypted_password: "a80104b33a0183e096f2ec82b3b9a8c4240fe61828ef5030e52...", 
salt: nil, 
avatar_file_name: nil, avatar_content_type: nil, 
avatar_file_size: nil, avatar_updated_at: nil>] 

Is there a way to get only the username of the user, instead of the whole hash?

Upvotes: 0

Views: 166

Answers (3)

Amokrane Chentir
Amokrane Chentir

Reputation: 30385

Just do:

  <% current_user.pending_invited.each do |u| %>
    <%= u.username %> 
  <% end %> 

Upvotes: 1

Brian
Brian

Reputation: 6840

Looks more like an array of objects to me (not familiar with the gem you are using).

Does something like this work:

<% current_user.pending_invited_by.each do |user| %>
  <%= user.username %>
<% end %>

Upvotes: 3

Wizard of Ogz
Wizard of Ogz

Reputation: 12643

From the Amistad docs it looks like evaluating pending_invited_by returns an array of users. So you actually need to iterate over those to get the first name for each one.

Pending: <%= current_user.pending_invited_by.map{|user| user.username }.join(", ") %>

Upvotes: 1

Related Questions