Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Rails 4.1.0.beta1: pg_search_scope produced invalid SQL #153

Closed
Darkside73 opened this issue Dec 28, 2013 · 8 comments
Closed

Rails 4.1.0.beta1: pg_search_scope produced invalid SQL #153

Darkside73 opened this issue Dec 28, 2013 · 8 comments

Comments

@Darkside73
Copy link

Given model Page with "title" attribute and search scope:

class Page < ActiveRecord::Base
  include PgSearch

  pg_search_scope :by_title, against: :title

end

This works as expected:

Page.by_title 'test'

But if call count method...

Page.by_title('test').count

PostgreSQL complains:

SELECT COUNT("pages".*, ((ts_rank((to_tsvector('ru', coalesce("pages"."title"::text, ''))), (to_tsquery('ru',
 ''' ' || 'test' || ' ''' || ':*')), 0))) AS pg_search_rank) FROM "pages"  WHERE (((to_tsvector('ru', coalesce
 ("pages"."title"::text, ''))) @@ (to_tsquery('ru', ''' ' || 'test' || ' ''' || ':*'))));

ERROR:  syntax error at or near "AS"
LINE 1: ...ry('ru', ''' ' || 'test' || ' ''' || ':*')), 0))) AS pg_sear...
@elsurudo
Copy link

elsurudo commented Jan 3, 2014

Having the same problem, also on 4.1.0.beta1.

@nertzy
Copy link
Collaborator

nertzy commented Jan 5, 2014

Interesting. I wonder if this is a problem with pg_search or with Rails. I've had to make some pull requests to Rails to fix similar issues in the past. I'll look into this soon.

@elsurudo
Copy link

elsurudo commented Jan 5, 2014

Thanks Grant. For now, a workaround (Post.all.to_a.count) will do, but it’s not ideal for large numbers of posts.

On Jan 5, 2014, at 5:28 PM, Grant Hutchins notifications@github.com wrote:

Interesting. I wonder if this is a problem with pg_search or with Rails. I've had to make some pull requests to Rails to fix similar issues in the past. I'll look into this soon.


Reply to this email directly or view it on GitHub.

nertzy added a commit that referenced this issue Jan 5, 2014
@nertzy
Copy link
Collaborator

nertzy commented Jan 5, 2014

I just read this Rails pull request rails/rails#10710 which is what introduced this problem.

It seems that the solution is to use count(:all) instead of count

Does this work for you?

Page.by_title('test').count(:all)

@nertzy
Copy link
Collaborator

nertzy commented Jan 5, 2014

#127 and #153 are related.

@nertzy
Copy link
Collaborator

nertzy commented Jan 5, 2014

Also, it seems that this commit was reverted on 4.0.0 Rails but not on master (where 4.1.0 is being developed). I'm unsure whether this was purposeful.

@Darkside73
Copy link
Author

Yes, it works for me:

Page.by_title('test').count(:all)

The problem is in rails API changed in 4.1.0.beta1.
Closing issue, thank for great gem

@samgrimm
Copy link

I was having the same issue and tried several things proposed here (will_paginate-bootstrap, kaminari, count(:all)). Finally what seems to have worked is Kaminari and getting rid of a "in_groups_of(3)" that I was calling in a view.

Hope this helps others!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants