How to list the users who LIKE a given Post

2019-06-13 22:07发布

问题:

I am having trouble figuring out how to list all of the USERS who have LIKED a particular POST. Currently I am using:

<span class="count"><%= dailypost.likes.count %></span>

This has allowed me to count all of the LIKES associated with that particular POST.BUT it does not list the actual users.

I have tried using this to list all of the USERS who LIKED that POST

<%= dailypost.likes %>

Instead I get back:

[#<Like id: 360, dailypost_id: 306, user_id: 1, created_at: "2013-04-28 21:51:45", updated_at: "2013-04-28 21:51:45">, 

#<Like id: 319, dailypost_id: 306, user_id: 104, created_at: "2013-04-28 19:27:35", updated_at: "2013-04-28 19:27:35">, 

#<Like id: 314, dailypost_id: 306, user_id: 103, created_at: "2013-04-28 19:24:19", updated_at: "2013-04-28 19:24:19">]

DOES ANYONE KNOW HOW I CAN PULL THE LIST OF USERS OR USER_ID'S??

New to RAILS please help!!

MODELS

class User < ActiveRecord::Base
  has_many :likes, dependent: :destroy
  has_many :dailyposts, dependent: :destroy
end

class Dailypost < ActiveRecord::Base
  belongs_to :user
  has_many :likes
end

class Like < ActiveRecord::Base
  belongs_to :user
  belongs_to :dailypost
end

DATABASE

ActiveRecord::Schema.define(:version => 20130210095553) do

create_table "dailyposts", :force => true do |t|
  t.string   "content"
  t.integer  "user_id"
  t.datetime "created_at",         :null => false
  t.datetime "updated_at",         :null => false
end

create_table "likes", :force => true do |t|
  t.integer  "dailypost_id"
  t.integer  "user_id"
  t.datetime "created_at",   :null => false
  t.datetime "updated_at",   :null => false
end

create_table "users", :force => true do |t|
  t.string   "name"
  t.string   "email"
  t.datetime "created_at",                                :null => false
  t.datetime "updated_at",                                :null => false
end

end

VIEWS

<span class="count"><%= dailypost.likes.count %></span>

<%= dailypost.likes %>

<span class="like">
 <% if like = current_user.likes.find_by_dailypost_id(dailypost.id) %>
   <%= form_for like, :html => { :method => :delete }, remote: true do |f| %>
     <span title="Unlike!"><%= image_submit_tag "Superman1.png" %></span>
   <% end %>
 <% else %>
 <%= form_for current_user.likes.build, remote: true do |f| %>
   <div><%= f.hidden_field :dailypost_id, value: dailypost.id %></div>
  <%= f.hidden_field :user_id %>
    <span title="Like!"><%= image_submit_tag "Superman2.png" %></span>
  <% end %>
 <% end %>
</span>

回答1:

The simplest way to understand is to take each like, and ask it for its user:

dailypost.likes.map {|l| l.user}

Of course that does a database lookup for each like, which you may wish to avoid if you have hundreds and hundreds of users.

So a more sophisticated way is to do a single query to get all the users by user_id:

User.find(dailypost.likes.map {|l| l.user_id})

This will give you a list of users, and it's up to you what you do with it. If you just want a HTML list, try this instead:

<ul>
<% dailypost.likes.each do |l| %>
     <li> <%= link_to(l.user.name, l.user) %></li>
<% end %>
</ul>

This looks at each like in turn, and then asks it for its user, and then asks the user for its name.