如何列出喜欢给定帖子的用户 [英] How to list the users who LIKE a given Post

查看:54
本文介绍了如何列出喜欢给定帖子的用户的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我不知道如何列出对特定帖子点赞的所有用户.目前我正在使用:

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>

这使我能够计算与该特定 POST 相关联的所有 LIKES.但它没有列出实际用户.

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

相反,我回来了:

[#<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">]

有谁知道我如何提取用户列表或 USER_ID 的列表?

RAILS 新手请帮忙!!

New to RAILS please help!!

模型

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

数据库

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

视图

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

推荐答案

最简单的理解方法是取每个like,并询问它的user:

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

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

当然,这会为每个 like 进行数据库查找,如果您有成百上千的用户,您可能希望避免这样做.

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

所以更复杂的方法是执行单个查询以通过 user_id 获取所有用户:

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})

这将为您提供一个用户列表,这取决于您如何使用它.如果你只想要一个 HTML 列表,试试这个:

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>

这会依次查看每个like,然后询问它的用户,然后询问用户的名称.

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

这篇关于如何列出喜欢给定帖子的用户的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆