简体   繁体   English

按分数对用户进行排序。 呈现数字而不是名称

[英]Sort users by score. Numbers being rendered instead of names

在此处输入图片说明

The database column for my users' names is first_name . 我的用户名的数据库列是first_name Score is not a column in the guides database. 得分不是指南数据库中的一列。

In my karma controller: 在我的业力控制器中:

  def hiscores
    @users = User.all.map(&:guides).flatten.map(&:score).sort
  end

In hiscores.html.erb 在hiscores.html.erb中

<h1>Karma Hiscores</h1>
<blockquote>Users sorted by most karma</blockquote>


<ul>
  <% @users.each do |user| %>
  <li><%= user %></li>
  <% end %>
</ul>

EDITS EDITS

Guide model 指导模型

class Guide < ActiveRecord::Base
  validates :link, uniqueness: true
  validates :link, presence: true
  validates :title, presence: true 

  belongs_to :user
  has_many :comments


  acts_as_taggable
  acts_as_votable

  def to_param
    "#{id} #{title}".parameterize
  end

  def score
    upvotes.count - downvotes.count
  end


end


User.rb

class User < ActiveRecord::Base
  # Include default devise modules. Others available are:
  # :confirmable, :lockable, :timeoutable and :omniauthable
  devise :database_authenticatable, :registerable, #:recoverable,
          :rememberable, :trackable, :validatable

  validates :first_name, presence: true
  validates :email, presence: true
  validates :email, uniqueness: true
  validates :runescape_username, presence: true

  has_many :guides
  has_many :comments

  acts_as_voter





end

Here is what my karma controller looks like now: 这是我的业力控制器现在的样子:

class KarmaController < ApplicationController
  def hiscores
    @users =  (guides.upvote.count - guides.downvote.count).desc
  end
end

In your karma controller: 在您的业力控制器中:

def hiscores
  @users = User.all.sort{ |x, y| y.user_score <=> x.user_score }
end

In your User.rb: 在您的User.rb中:

If each user has_many: guides, then 如果每个用户都有has_many:指南,则

def user_score
  self.guides.inject(0) { |sum, guide| sum += guide.score }
end

Else if each user has_one: guide, then 否则,如果每个用户都有has_one:指南,则

def user_score
  self.guide.score
end

How about something like this: 这样的事情怎么样:

@users = User.find(:all, include: [:guides], order: "(guides.upvotes.count - guides.downvotes.count) desc")

I am not sure if include is required, I am typing from the top of my head :) 我不确定是否需要包含,我在脑海中打字:)

Edit: 编辑:

You should try to use Caching from acts_as_votable . 您应该尝试从acts_as_votable使用缓存

This would give you fields such as cached_votes_up and cached_votes_down and cached_votes_score . 这将为您提供诸如cached_votes_upcached_votes_downcached_votes_score

Then you could query users as 然后您可以查询用户为

@users = User.find(:all, include: [:guides], order: "guides.cached_votes_score desc")

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

相关问题 Javascript不在脚本中呈现,而是在HTML中呈现 - Javascript not being rendered at script but as html instead 管理未正确呈现的gem STI模型名称 - Administrate gem STI model names not being rendered correctly 减少 Rails 应用程序中的 Javascript 大小并提高性能得分。 也许 ESBuild 有问题? - Reduce Javascript size in Rails app and improve performance score. Maybe issue with ESBuild? 为什么这个HTML代码被渲染为一个巨大的字符串而不是被评估的代码? - Why is this HTML code being rendered as one giant string instead of the code being evaluated? 使用solr按名称的字母顺序对用户进行Rails排序 - Rails- sort users in alphabetical order of their names using solr 按分数对用户排序-Rails - Sorting users by score - rails Ruby on Rails 应用程序 Redmine 返回文本/纯文本 header 导致页面被下载而不是被呈现 - Ruby on Rails application Redmine returns text/plain header causing pages to be downloaded instead of being rendered 用户名未显示在用户视图中 - users names not showing in users view 视图呈现时没有布局 - View is being rendered without layout Rails 3:我的视图没有渲染 - Rails 3: My view is not being rendered
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM