Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/ruby/23.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails Rails 4:尝试根据计算值对资源进行排序时是否会出现分页错误_Ruby On Rails_Ruby_Sorting_Ruby On Rails 4_Will Paginate - Fatal编程技术网

Ruby on rails Rails 4:尝试根据计算值对资源进行排序时是否会出现分页错误

Ruby on rails Rails 4:尝试根据计算值对资源进行排序时是否会出现分页错误,ruby-on-rails,ruby,sorting,ruby-on-rails-4,will-paginate,Ruby On Rails,Ruby,Sorting,Ruby On Rails 4,Will Paginate,我使用Rails 4.1.0,我想根据完整性对配置文件进行排序,因此我在配置文件模型中有了这段代码 def completeness percent = 10 percent += 15 if self.useravatar.present? percent += 5 if self.summary.present? percent += 5 if self.profile_languages.present? percent += 10 if self

我使用Rails 4.1.0,我想根据完整性对配置文件进行排序,因此我在配置文件模型中有了这段代码

def completeness
    percent = 10

    percent += 15 if self.useravatar.present?
    percent += 5 if self.summary.present?
    percent += 5 if self.profile_languages.present?
    percent += 10 if self.educations.present?
    return percent
end

def self.sorted_by_completeness
  Profile.all.sort_by(&:completeness).reverse
end
这是我的配置文件索引控制器

  def index
    @profiles = Profile.paginate(page: params[:page], per_page: 10).sorted_by_completeness
  end 
但是当我试图访问配置文件索引页面时,我得到了这个错误
未定义的方法
total_pages'for#`有人能告诉我为什么会出现这个错误吗

 def index
  @profiles = Profile.sorted_by_completeness(params[:page])
 end
在模型文件中:

def self.sorted_by_completeness(current_page)
    Profile.all.sort_by(&:completeness).reverse.paginate(page: current_page)
end
 #app/config/initializers/will_paginate.rb 
  WillPaginate.per_page = 10
要定义每页的限制,请使用以下代码:

def self.sorted_by_completeness(current_page)
    Profile.all.sort_by(&:completeness).reverse.paginate(page: current_page)
end
 #app/config/initializers/will_paginate.rb 
  WillPaginate.per_page = 10
如果要在模型级别定义它,请执行以下操作:

  #profile.rb
   class Profile < ActiveRecord::Base
      self.per_page = 10
    #your other model level logic goes here.
    end
#profile.rb
类配置文件
排序依据
将返回
数组
集合

测试开放式rails控制台和类型

Profile.all.sort_by(&:completeness).reverse.class
要使用数组,您需要
要求“will\u paginate/Array”
因此,只需将您的profile_控制器更改为

require 'will_paginate/array'
class ProfileController < ApplicationController
  def index
    @profiles = Profile.sorted_by_completeness.paginate(page: params[:page], per_page: 10)
  end 
end
require'will_paginate/array'
类ProfileController<应用程序控制器
def索引
@配置文件=配置文件。按完整性排序。分页(第页:参数[:页],每页:10)
结束
结束

您的方法存在两个问题:

  • 在您看来,您可能正在使用
    will\u paginate(@projects)
    ,但该方法失败,因为该方法不适用于数组

  • 您正在对分页后的行进行排序。这意味着,无论您如何对它们进行排序,排序一次只能应用于一个页面。因此,无论选择何种解决方案,都需要在对数据分页之前指定顺序

  • 解决方案显然是在数组上使用分页。比如:

    require 'will_paginate/array' # to enable pagination on an array
    @projects = Project.sorting_method.paginate(arguments)
    
    lcq = "SELECT COUNT(*) FROM profile_languages WHERE
      (profile_languages.project_id = projects.id)"
    ecq = "SELECT COUNT(*) FROM educations WHERE
      (educations.project_id = projects.id)"
    
    @projects = \
      Project.select(
        "#{project.column_names.join(',')},
        SELECT(
          (CASE useravatar WHEN NULL THEN 0 ELSE 15 END) +
          (CASE summary WHEN NULL THEN 0 ELSE 5 END) +
          (CASE (#{lcq}) WHEN 0 THEN 0 ELSE 5 END) +
          (CASE (#{ecq}) WHEN 0 THEN 0 ELSE 10 END)
        ) AS completeness").
      order("completeness DESC").
      paginate(arguments)
    
    然而,对于许多项目来说,这很快就会变得不切实际

    IMO启用分页的最佳方法是在
    项目
    表中有一个
    完备性
    字段,该字段可以随时更新。然后,您可以非常轻松地执行以下操作:

    @projects = Project.order("completeness DESC").paginate(arguments)
    
    另一种解决方案是在SQLSELECT语句中使用子查询来计算每个项目的完整性。比如:

    require 'will_paginate/array' # to enable pagination on an array
    @projects = Project.sorting_method.paginate(arguments)
    
    lcq = "SELECT COUNT(*) FROM profile_languages WHERE
      (profile_languages.project_id = projects.id)"
    ecq = "SELECT COUNT(*) FROM educations WHERE
      (educations.project_id = projects.id)"
    
    @projects = \
      Project.select(
        "#{project.column_names.join(',')},
        SELECT(
          (CASE useravatar WHEN NULL THEN 0 ELSE 15 END) +
          (CASE summary WHEN NULL THEN 0 ELSE 5 END) +
          (CASE (#{lcq}) WHEN 0 THEN 0 ELSE 5 END) +
          (CASE (#{ecq}) WHEN 0 THEN 0 ELSE 10 END)
        ) AS completeness").
      order("completeness DESC").
      paginate(arguments)
    
    通过一些调试,这应该可以工作。不过我不喜欢。一堆难以阅读的代码和子查询可能会大大降低速度