简体   繁体   English

自定义方法顶部的rails gem will_paginate不起作用

[英]rails gem will_paginate on top of custom method not working

In my controller, my filter_with_params() method is causing a syntax error in postgres when I try and stack will_paginate on top of it. 在我的控制器中,当我尝试在其顶部堆叠will_paginate时,我的filter_with_params()方法在postgres中导致语法错误。

In my controller I call: 在我的控制器中,我致电:

@styles = Style.filter_with_params(params).paginate(:page => params[:page], :per_page => 6)

Model method: 建模方法:

class Style < ActiveRecord::Base

def self.filter_with_params(params)
    scoped = where("styles.item != ''")
    if params[:t]
      scoped = scoped.joins(:tags)
      scoped = scoped.select("distinct styles.*, count(*) AS count")
      scoped = scoped.where(tags: { name: params[:t] })
      scoped = scoped.group('styles.id')
      scoped = scoped.having("count(*) = #{params[:t].size}")
    end
    scoped
end

basically my filter method is looking for tags, and then i need to paginate on top of those results. 基本上我的过滤器方法正在寻找标签,然后我需要在这些结果上进行分页。 Anyone with similar experience? 有类似经验的人吗?

I'm using Rails 3 on this app 我正在此应用上使用Rails 3

here is the postgres error 这是postgres错误

PG::Error: ERROR:  syntax error at or near "distinct" LINE 1: SELECT COUNT(*) AS count_all, distinct styles.*, count(*) AS...
                                  ^
: SELECT COUNT(*) AS count_all, distinct styles.*, count(*) AS count, styles.id AS styles_id FROM "styles" INNER JOIN "tagizations" ON "tagizations"."style_id" = "styles"."id" INNER JOIN "tags" ON "tags"."id" = "tagizations"."tag_id" WHERE "tags"."name" IN ('engagement') AND (styles.polenza_item != '') GROUP BY styles.id HAVING count(*) = 1

Your SQL has a problem. 您的SQL有问题。 You need to say distinct clause before the count ('count(*) as count_all'). 您需要在计数之前说出不同的子句('count(*)as count_all')。 That is, once you remove the first call to the count function, it should work. 也就是说,一旦删除了对count函数的第一个调用,它就应该起作用。

SELECT distinct styles.*, count(*) AS count, styles.id AS styles_id FROM "styles" INNER JOIN "tagizations" ON "tagizations"."style_id" = "styles"."id" ...

You can test your query in your rails console: 您可以在Rails控制台中测试查询:

>> sql = "SELECT distinct styles.*, count(*) AS count, styles.id AS styles_id..."
>> a = ActiveRecord::Base.connection.execute(sql)
>> a[0]

Hope this helps. 希望这可以帮助。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM