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

Wrong result and errors for join/group/having scope with certain values (0 and 1) #502

Closed
gkop opened this issue Jan 28, 2015 · 7 comments
Closed

Comments

@gkop
Copy link

gkop commented Jan 28, 2015

Testing ransack v1.6.3, rails 4.1.8

class Cart < ActiveRecord::Base
  has_many :items

  def self.items_count_equals(n)
    joins(:items).group("carts.id").having("count(items.id) = ?", n)
  end

  def self.ransackable_scopes
    [:items_count_equals]
  end
end

# when 0 is passed in to ransack, no filtering is performed, and all Cart records are
# returned. (in fact, the scope method is never called)
# meanwhile the class method *does* produce correct results with 0 passed in
> Cart.ransack(items_count_equals: 0).result.length == Provider.items_count_equals(0).length
=> false

# when 1 is passed in to ransack, the scope method is called
# but `true` comes in for `n` which is eventual converted to
# a "t", which causes a SQL error
> Cart.ransack(items_count_equals: 1).result
PG::InvalidTextRepresentation: ERROR:  invalid input syntax for integer: "t"
LINE 1: ...rts"."id" GROUP BY carts.id HAVING count(items.id) = 't'

Passing in positive integer values other than 0 and 1 appears to produce the expected results.

This unexpected behavior also appears when using named scopes rather than class methods and when querying for < and > in addition to =.

When using named a named scope and a lambda, the error is different in the case when 1 is passed in:

ArgumentError: wrong number of arguments (0 for 1)

This suggests to me this bug may be connected to #404 .

@gkop gkop changed the title Wrong result or errors for join/group/having scope with certain values (0 and 1) Wrong result and errors for join/group/having scope with certain values (0 and 1) Jan 28, 2015
gkop pushed a commit to Everseat/ransack that referenced this issue Jan 28, 2015
@dan532
Copy link

dan532 commented Jan 29, 2015

+1 here but looks like fix 502 will see us through. Thank you!

jonatack added a commit that referenced this issue Jan 30, 2015
Don't replace 0 and 1 with false and true, fixes #502 [skip ci]
@jaredbeck
Copy link
Contributor

I think this commit (9f4e48e) might break a really common use case.

Let's say there's a checkbox like:

<input name="search[my_bool_eq]" value="1">

And that value goes straight into a search, like:

MyRecord.search(params['search'])

Before this commit, search(my_bool: '1') only finds true records. After this commit, it finds all records.

So, this commit breaks a really common use case, right? We'd have to do some manual typecasting in our controller now. Was that an intentional change? If so, I'd recommend bumping the major version of ransack, because I think it's gonna break a lot of apps out there.

CC: @jonatack (Do you want a new issue for this?)

@avit
Copy link
Contributor

avit commented Jan 31, 2015

@jonatack please see my comment on the pull request #503

@gkop
Copy link
Author

gkop commented Jan 31, 2015

@avit so I just change my code to this?

Cart.ransack(items_count_equals: [1]).result

@avit
Copy link
Contributor

avit commented Jan 31, 2015

@gkop yup, that's right!

@gkop
Copy link
Author

gkop commented Jan 31, 2015

Ah excellent. I suppose I will need to patch activeadmin now :)

@tekniklr
Copy link

It seems that this is also a problem when the strings 't' or 'f' are passed in.

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

5 participants