分页前是否可以按条件过滤?

发布于 2024-09-05 20:19:57 字数 2902 浏览 5 评论 0原文

我正在使用 ruby​​ on Rails 2.3.8 和 will_paginate 插件。

我刚刚注意到,如果我写这样的东西:

Announcement.paginate :page => params[:page], :per_page => 10, :conditions => some_condition

它会起作用。

但是,如果我写这样的东西:

announcements = Announcement.all :conditions => some_condition
@ann = announcements.paginate :page => params[:page], :per_page => 10

它不会识别条件。

编辑:

我开发了一个搜索功能,并且由于我必须实现排序功能,我必须将搜索功能放入模型的方法中,以便每次需要时从控制器调用它按某个字段搜索或排序。

因此,我的模型的方法如下所示:

  def self.search_by_relevance(words)
    conditions = get_search_conditions(words)

    Announcement.published.descend_by_featured.order_by_rate :conditions => conditions
  end

其中“published”和“order_by_rate”被命名为范围,“descend_by_feature”属于“searchlogic”gem。

  def self.get_search_conditions(words)
    unless words.empty? or words.nil?
      conditions = ''

      words.each do |word|

        if conditions.nil? or conditions.empty?
          conditions = '(title  like "%' + word + '%" or description  like "%' + word + '%")'
        else
          conditions += ' and (title  like "%' + word + '%" or description  like "%' + word + '%")'
        end
      end

      conditions
    end
  end

我的控制器的操作如下所示:

def search
  @announcements = Announcement.search_by_relevance(params[:txtSearch].to_s.split).paginate :page => params[:page], :per_page => 10 unless params[:txtSearch].nil? or params[:txtSearch].empty?
end

此语法无法识别模型方法中指定的条件。

编辑2:

感谢您的帖子。进一步测试我的代码,我发现如果我在这一行的“order_by_rate”之后写“.all” Announcement.published.descend_by_featured.order_by_rate :conditions =>条件,在 search_by_relevance 方法中它将返回正确的查询,但是 will_paginate 插件会给我以下错误(只要我添加“.all”):

NoMethodError in AnnouncementsController#search

undefined method `to_i' for {:page=>nil, :per_page=>10}:Hash

D:/Proyectos/Cursometro/www/vendor/plugins/will_paginate/lib/will_paginate/collection.rb:15:in `initialize'
D:/Proyectos/Cursometro/www/vendor/plugins/will_paginate/lib/will_paginate/core_ext.rb:37:in `new'
D:/Proyectos/Cursometro/www/vendor/plugins/will_paginate/lib/will_paginate/core_ext.rb:37:in `paginate'
D:/Proyectos/Cursometro/www/app/controllers/announcements_controller.rb:276:in `search'

首先,我不明白为什么我必须添加查询中的“.all”可以正常工作,其次,我不明白为什么当我包含“.all”时 will_paginate 不起作用(我也尝试添加以下代码但不起作用::page => params[:page] || 1)。

另外,如果我在查询中包含“.all”语法,它将返回:

公告中选择*,其中 ((标题如“%anuncio%”或 描述如“%anuncio%”))和 (announcements.state = '已发布') 按公告排序。精选 DESC

如果我不这样做,它将返回:

公告中选择*,其中 (announcements.state = '已发布') 按公告排序。精选 DESC

您是否看到最后一个条件中没有包含任何条件?这就是导致问题的原因。

I'm using ruby on rails 2.3.8 and will_paginate plugin.

I've just noticed that if I write something like this:

Announcement.paginate :page => params[:page], :per_page => 10, :conditions => some_condition

it will work.

But, if I write something like this:

announcements = Announcement.all :conditions => some_condition
@ann = announcements.paginate :page => params[:page], :per_page => 10

it won't recognize conditions.

EDIT:

I've developed a Search functionality and, due to a Sort functionality I had to implement, I had to put the search feat inside a model's method to call it from the controller every time I need either to search or sort by some field.

So, my model's methods look like this:

  def self.search_by_relevance(words)
    conditions = get_search_conditions(words)

    Announcement.published.descend_by_featured.order_by_rate :conditions => conditions
  end

where "published" and "order_by_rate" are named scopes and "descend_by_feature" belongs to "searchlogic" gem.

  def self.get_search_conditions(words)
    unless words.empty? or words.nil?
      conditions = ''

      words.each do |word|

        if conditions.nil? or conditions.empty?
          conditions = '(title  like "%' + word + '%" or description  like "%' + word + '%")'
        else
          conditions += ' and (title  like "%' + word + '%" or description  like "%' + word + '%")'
        end
      end

      conditions
    end
  end

My controller's action looks like this:

def search
  @announcements = Announcement.search_by_relevance(params[:txtSearch].to_s.split).paginate :page => params[:page], :per_page => 10 unless params[:txtSearch].nil? or params[:txtSearch].empty?
end

This syntax won't recognize the conditions specified in the model's method.

EDIT 2:

Thanks for the posts. Testing my code a little more I found out that if I write ".all" right after "order_by_rate" at this line Announcement.published.descend_by_featured.order_by_rate :conditions => conditions, in search_by_relevance method it will return the correct query, but will_paginate plugin will give me the following error(just if I add ".all"):

NoMethodError in AnnouncementsController#search

undefined method `to_i' for {:page=>nil, :per_page=>10}:Hash

D:/Proyectos/Cursometro/www/vendor/plugins/will_paginate/lib/will_paginate/collection.rb:15:in `initialize'
D:/Proyectos/Cursometro/www/vendor/plugins/will_paginate/lib/will_paginate/core_ext.rb:37:in `new'
D:/Proyectos/Cursometro/www/vendor/plugins/will_paginate/lib/will_paginate/core_ext.rb:37:in `paginate'
D:/Proyectos/Cursometro/www/app/controllers/announcements_controller.rb:276:in `search'

First of all, I don't understand why I have to add the ".all" to the query to work right, and second, I don't see why will_paginate won't work when I include ".all"(I also tried to add the following code but didn't work: :page => params[:page] || 1).

Also, if I include the ".all" syntax to the query, it will return:

SELECT * FROM announcements WHERE
((title like "%anuncio%" or
description like "%anuncio%")) AND
(announcements.state = 'published')
ORDER BY announcements.featured DESC

If I don't, it will return:

SELECT * FROM announcements WHERE
(announcements.state = 'published')
ORDER BY announcements.featured DESC

Do you see that no conditions are being included in the last one? This is causing the problem.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

顾北清歌寒 2024-09-12 20:19:57

我不知道这是否适合您,但是您可以使用 paginate 就像 find 一样,我的意思是,类似于:

@announcements = Announcement.paginate_all_by_id params[:id], 
   :page => params[:page], :per_page => 10

编辑

< code>@announcements 是一个数组,对吗?

好吧,我发现这个 帖子和 这个其他可能对您有帮助的。

I don't know if this will work for you, but you can use paginate just like find, I mean, something like:

@announcements = Announcement.paginate_all_by_id params[:id], 
   :page => params[:page], :per_page => 10

Edit

@announcements is an array, right?

Well, I found this post and this other one that may help you.

小傻瓜 2024-09-12 20:19:57

好吧,我通过在模型方法中的查询中添加“.paginate”(而不是“.all”)来解决这个问题,并通过参数传递“page”和“per_page”值。在模型中包含分页并不是我的想法,但是……这是我目前的解决方案。如果您想出更好的方案,我将很高兴听到:)

Well, I kind of solve this by adding ".paginate"(instead of ".all") to my query in the model's method, passing by parameters the "page" and "per_page" values. It was not my idea to include pagination in models, but well...it's the solution I have for now. If you come up with a better one, I'll be glad to hear it :)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文