Ruby on rails Rails 4:尝试根据计算值对资源进行排序时是否会出现分页错误
我使用Rails 4.1.0,我想根据完整性对配置文件进行排序,因此我在配置文件模型中有了这段代码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
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)
通过一些调试,这应该可以工作。不过我不喜欢。一堆难以阅读的代码和子查询可能会大大降低速度