【问题标题】:Ruby on Rails - Search, Order, Limit then PaginateRuby on Rails - 搜索、排序、限制然后分页
【发布时间】:2013-12-10 15:06:55
【问题描述】:

我想要一个数据搜索功能。搜索后,按列排序,然后将结果限制为 200。最后用 will_paginate 分页。

我的控制器

  def search
    title = params[:title]
    company = params[:company]
    location_id = params[:location_id]
    page = params[:page]
    @wages = Wage.search(title, company, location_id,page)
  end

我的模型

def self.search(title, company, location_id, page) 如果 location_id.present?

    paginate :conditions => ['title LIKE ? AND company LIKE ? AND location_id = ?', "%#{title}%", "%#{company}%", location_id],
                    :order => "total DESC",
                    :page => page,
                    :per_page => 20                       
else

    paginate :conditions => ['title LIKE ? AND company LIKE ?', "%#{title}%", "%#{company}%"],
                    :order => "total DESC",
                    :page => page,
                    :per_page => 20                                                                    
end

结束

我尝试更改为以下代码以限制结果:

paginate :conditions => ['title LIKE ? AND company LIKE ?', "%#{title}%", "%#{company}%"].limit(200)

但它不起作用。最好的方法是什么?

【问题讨论】:

标签: ruby-on-rails ruby pagination


【解决方案1】:

在你的模型中

class Wage < ActiveRecord::Base
  self.per_page = 10

  def self.search(title, company, location_id, page)

    if location_id     
      wages = Wage.where('title LIKE ? AND company LIKE ? AND location_id = ?', "%#{title}%", "%#{company}%", location_id).order('total DESC').limit(200).paginate(:page => page)
    else
      wages = Wage.where('title LIKE ? AND company LIKE ?', "%#{title}%", "%#{company}%").order('total DESC').limit(200).paginate(:page => page)
    end
    return wages
  end
end

或者,您可以将“total_entries”传递给您的分页方法,如下所示:

wages = Wage.where('title LIKE ? AND company LIKE ? AND location_id = ?', "%#{title}%", "%#{company}%", location_id).order('total DESC').paginate(:page => page, :total_entries => 200)

我希望这会有所帮助。

【讨论】:

  • 您应该确保您拥有与标题和公司的搜索词相匹配的任何数据。代码应该可以正常工作。
  • 我稍微改了一下代码。它可以很好地执行搜索,但 .limit(200) 功能不起作用。它仍然显示所有数据。
  • 限制方法,由 Rails 的 ActiveRecord 提供,应该可以工作。您可以查看 Rails 日志以查看实际查询,并注意该方法生成的 SQL 应包含“LIMIT 200”。你的数据库实现是什么? SQLite3、MySQL 还是 PG?尽管如此,我还是编辑了我的答案以显示其他方式,即使用“total_entries”。
【解决方案2】:

检查另一个问题will-paginate-limit-number-of-results 似乎你可以做这样的事情:

wages = Wage.limit(100).paginate(:per_page => 5)

在你的情况下(我没有测试过,但我认为它可以工作):

@wages = Wage.where('title LIKE ? AND company LIKE ?', "%#{title}%", "%#{company}%").limit(200).paginate(:per_page => 20, :order => "total DESC", :page => page)

在这种情况下,@wages 是一个 will_paginate 对象:

@wages.total_pages
=> 20

或者您可以在视图中使用will_paginate 的html_helpers 进行渲染 (API-documentation#view-template-helpers)。

# controller
@wages = Wage.where('title LIKE ? AND company LIKE ?', "%#{title}%", "%#{company}%").limit(200)
# you should have @wages.count = 200

# view
<%= will_paginate @wages, :style => 'color:blue' %>

【讨论】:

  • 我尝试了其他答案中提到的类似方法。但是 .limit(200) 函数没有执行,因为它仍然显示所有数据。
  • @NgYatPing 听起来有点奇怪,第二个例子是一个简单的查询 + will_paginate,你能数一下吗(编辑我的答案)?您确定传递给查询的参数是您想要的吗?仅尝试独立查询
猜你喜欢
  • 2013-12-23
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-02-19
  • 1970-01-01
  • 2012-07-23
  • 1970-01-01
相关资源
最近更新 更多