开发者

Show only username of user with ruby on rails

开发者 https://www.devze.com 2023-04-12 06:27 出处:网络
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 fe

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: "zxczxc@zxczxc.zxc", 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?


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 %>


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(", ") %>


Just do:

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

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号