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

Getting unexpected coercion within scopes. #566

Closed
alejandrodevs opened this issue Jul 15, 2015 · 10 comments
Closed

Getting unexpected coercion within scopes. #566

alejandrodevs opened this issue Jul 15, 2015 · 10 comments

Comments

@alejandrodevs
Copy link

I know that it is a discussed issue but I'm still getting errors.
Example:

class Payment < ActiveRecord::Base
  scope :by_month, -> (month) { where('extract(month from created_at) = ?', month) }
  def self.ransackable_scopes(object)
    [:by_month]
  end
end

Payment.ransack(by_month: 1)     # I get true param in scope.
Payment.ransack(by_month: '1')    # I get true param in scope.
Payment.ransack(by_month: [1])   # I get true param in scope.
Payment.ransack(by_month: ['1'])  # I get true param in scope.

ruby 2.2.2
rails 4.2.1
ransack 1.6.6

I want to catch 1 or '1' as param. Any idea?

@MarcelEeken
Copy link
Contributor

I have the same problem currently here.

Where i expect value such as 1 or true to be passed as string to my scope, they are converted to a boolean true. Even if they are enclosed in an array.

@MarcelEeken
Copy link
Contributor

While looking further in to this I discovered that the way you check for scope arity doesn't work in rails 4.
You check scope arity by using the following method: https://github.com/activerecord-hackery/ransack/blob/master/lib/ransack/context.rb#L53 which uses the arity method of a ruby method object.

If you look how scopes are implemented currently: https://github.com/rails/rails/blob/b1879124a82b34168412ac699cf6f654e005c4d6/activerecord/lib/active_record/scoping/named.rb#L154 you see that is defines a method, where the block passed is the scope method from rails which executes our own specified proc. So when you ask arity for the scope, you are actually asking for arity of the scope block from rails, which is always -1.

@jonatack
Copy link
Contributor

@MarcelEeken thanks for looking into this. I use ransackers as they were the original implementation by Ernie for doing this. Scopes in Ransack were added much later by the community but never quite finished/polished. The early users of Ransack probably mostly stayed with their ransackers, but I wouldn't be surprised if many newer users are using scopes. So, tests and PRs for using scopes are very welcome 😃

@jonatack
Copy link
Contributor

I believe that yes, this issue has been discussed in depth at least once before. Do have a look, and don't hesitate to link this issue to the previous discussion.

@MarcelEeken
Copy link
Contributor

I started looking into it, but haven't found a solution yet. I have a red test case, but have not been able to think of a solution that doesn't clash with the way of conditionally adding a scope with a true or false value.

One of the problems I think is, there is no way of determining if a scope needs arguments or not because of the wrapper rails adds to it with the current setup.

@jonatack
Copy link
Contributor

Could you make a PR with the test case ? (commented out, and headed by a TODO comment)

@MarcelEeken
Copy link
Contributor

I will do that.
See pull request #575

@jonatack
Copy link
Contributor

Thanks!

jonatack pushed a commit that referenced this issue Sep 6, 2015
- Small typo fix (@MarcelEeken).

- Update the TODO comment (@jonatack).

These tests are related to issue #566. Closes PR #575.
@avit
Copy link
Contributor

avit commented Feb 17, 2016

Also related to #509. See my comments, I would like to standardize on Array = passed params (unconverted), and single value = use the scope if truthy. If that makes sense I will implement the fix.

@scarroll32
Copy link
Member

Closed by #566

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