简体   繁体   English

如何计算用户对我的特定物品的喜欢程度(工资)

[英]How to count all the likes that User has done on my specific items (Rails)

My apologies for the newbie question, I'm relatively new to Rails and SQL. 我为新手问题道歉,我对Rails和SQL比较陌生。 I'm trying to count all the likes a User has done on my specific items. 我正在尝试计算用户对我的特定物品的喜欢程度。 With the help of the SO community and from Treehouse Coding I am able to show which Users have liked my specific items. 在SO社区和Treehouse Coding的帮助下,我可以显示哪些用户喜欢我的特定物品。 In addition, I was able to research how to show all the likes that I have received (from Lisa, James, Harry, etc.) using .join in my controller. 此外,我能够研究如何在控制器中使用.join展示我收到的所有喜欢(来自Lisa,James,Harry等)的赞。 Now I would like to show the count for each specific User. 现在,我想显示每个特定用户的数量。 So for instance if James like 4 of my items I would like to show 4 next to James. 例如,如果詹姆斯喜欢我的4个物品,我想在詹姆斯旁边显示4。 I have listed all my relevant code below, thank you guys so much!! 我在下面列出了我所有的相关代码,非常感谢你们!!

Index.html.erb Index.html.erb

My Fans - <%= @likersnumero%>
<%- @likers.each do |liker| %>
<%= image_tag liker.avatar, width: 25, class: "css-style"  %>
<%= liker.username %>
<% end %>

Items_controller Items_controller

def index
@items = Item.order("created_at DESC")
if current_user.present? 
@likers = current_user.items.map(&:likes).flatten.map(&:user).flatten
@likersnumero = current_user.items.joins(:likes).map(&:user).count
end
end

Item.rb Item.rb

class Item < ApplicationRecord
has_many :likes, :counter_cache => true
end

Users.rb Users.rb

class User < ApplicationRecord
has_many :likes
def likes?(post)
    post.likes.where(user_id: id).any?
end

#Tried using def total_likes in my index.html.erb using <%= liker.total_likes%>
but got the following
 error SQLite3::SQLException: no such column: likes:
 SELECT SUM(likes) FROM "likes" WHERE "likes"."user_id" = ?"
def total_likes
    likes.sum(:likes)   
end
end

Routes.rb Routes.rb

Rails.application.routes.draw do
resources :items do
resource :like, module: :items
end
root to: "items#index"
end

Schema.rb 模式.rb

create_table "items", force: :cascade do |t|
t.string   "product"
t.integer  "user_id"
t.integer  "item_id"
t.datetime "created_at",                      null: false
t.datetime "updated_at",                      null: false
t.string   "avatar_file_name"
t.string   "avatar_content_type"
t.integer  "avatar_file_size"
t.datetime "avatar_updated_at"
t.integer  "likes_count",         default: 0, null: false
end

create_table "likes", force: :cascade do |t|
t.integer  "user_id"
t.integer  "item_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end

Given liker is the user who liked post and poster_id is the id of the person who create the post. 鉴于liker是谁喜欢柱和用户poster_id是谁创造职位的人的ID。

liker.likes.joins(:items).where('items.user_id = ?', poster_id).count

Or somethings similar. 或类似的东西。 If you post your annotated model or table schema I can probably come up with a better answer. 如果您发布带注释的模型或表模式,则可能可以提供更好的答案。

After countless reading and experiementing I figured out the answer that worked for me. 经过无数次阅读和实验后,我找出了对我有用的答案。 In case anyone might encounter a similar issue - I have provided the answer that worked for me. 万一有人可能遇到类似的问题-我提供了对我有用的答案。

To get total likes by a specific user on all users items - In my controller I put the following @likersuno = @likers.map(&:likes).flatten.map(&:user).flatten.uniq 要获得特定用户对所有用户项目的总喜欢-在我的控制器中,我将以下@likersuno = @ likers.map(&:likes).flatten.map(&:user).flatten.uniq

And in my index.html.erb 在我的index.html.erb中

I simply put this and it worked from there 我简单地把它放在那里就可以了

<%= @likersuno.count %> 

To get total likes by a specific user on my specific items - In my index.html.erb I put the following and it worked beautifully 为了获得特定用户对我的特定商品的总喜欢-在我的index.html.erb中,我输入了以下内容,效果很好

<%= liker.likes.select { |like| like.item.user == current_user }.count %>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM