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

1.6.0 works, but master has NoMethodError: undefined method 'val' #504

Closed
jaredbeck opened this issue Jan 30, 2015 · 13 comments
Closed

1.6.0 works, but master has NoMethodError: undefined method 'val' #504

jaredbeck opened this issue Jan 30, 2015 · 13 comments

Comments

@jaredbeck
Copy link
Contributor

NoMethodError: undefined method `val' for 
#<Arel::Nodes::Quoted:0x007fd3740118b0 @expr=nil>

This issue seems to have been introduced in 1.6.1 by 6c59e96

If I revert the following change to adapters/active_record/ransack/nodes/condition.rb from said commit, then my tests pass again.

-          predicate.right.is_a?(Array) &&
-          predicate.right[0].class == Arel::Nodes::Casted
+          predicate.right[0].val.is_a?(Array)

Of course, I'm not suggesting we revert the change, just trying to help isolate the issue.

Stack trace:

NoMethodError: undefined method `val' for #<Arel::Nodes::Quoted:0x007fc1d4872678 @expr=nil>
..snip..lib/ransack/adapters/active_record/ransack/nodes/condition.rb:48:in `casted_array_with_in_predicate?'
..snip..lib/ransack/adapters/active_record/ransack/nodes/condition.rb:37:in `block in format_predicate'
..snip..lib/ransack/adapters/active_record/ransack/nodes/condition.rb:36:in `tap'
..snip..lib/ransack/adapters/active_record/ransack/nodes/condition.rb:36:in `format_predicate'
..snip..lib/ransack/adapters/active_record/ransack/nodes/condition.rb:23:in `arel_predicate_for'
..snip..lib/ransack/adapters/active_record/ransack/nodes/condition.rb:6:in `arel_predicate'
..snip..lib/ransack/visitor.rb:17:in `visit_Ransack_Nodes_Condition'
..snip..lib/ransack/visitor.rb:52:in `visit'
..snip..lib/ransack/visitor.rb:5:in `accept'
..snip..lib/ransack/adapters/active_record/ransack/visitor.rb:4:in `block in visit_and'
..snip..lib/ransack/adapters/active_record/ransack/visitor.rb:4:in `map'
..snip..lib/ransack/adapters/active_record/ransack/visitor.rb:4:in `visit_and'
..snip..lib/ransack/visitor.rb:24:in `visit_Ransack_Nodes_Grouping'
..snip..lib/ransack/visitor.rb:52:in `visit'
..snip..lib/ransack/visitor.rb:5:in `accept'
..snip..lib/ransack/adapters/active_record/context.rb:36:in `evaluate'
..snip..lib/ransack/search.rb:43:in `result'
.. 
.. etc. N/A

Relevant gems:

  • arel 6.0.0
  • activerecord 4.2.0 from git://github.com/rails/rails.git (at 4-2-stable)
@jonatack
Copy link
Contributor

Thank you @jaredbeck. Could you provide the query or test that is causing this issue (and the Ruby version just in case)?

@jaredbeck
Copy link
Contributor Author

Sure, I don't know much about ransack, but I'll try. When I inspect the Ransack::Search object just prior to calling result, it looks like this:

pry> search = REDACTED.search(params[:search])
=> Ransack::Search<class: REDACTED, 
base: Grouping <conditions: [
  Condition <attributes: ["REDACTED_id"], predicate: eq, values: ["something"]>, 
  Condition <attributes: ["with_unit"], predicate: in, values: ["something"]>, 
  Condition <attributes: ["REDACTED_id"], predicate: eq, values: ["something"]>, 
  Condition <attributes: ["REDACTED_id"], predicate: eq, values: ["something"]>, 
  Condition <attributes: ["REDACTED_name"], predicate: eq, values: ["something"]>, 
  Condition <attributes: ["REDACTED_id"], predicate: eq, values: ["something"]>, 
  Condition <attributes: ["REDACTED_id"], predicate: eq, values: ["something"]>
], combinator: and>>
pry> search.result
NoMethodError: undefined method `val' for #<Arel::Nodes::Quoted:0x007fc2ae083db0 @expr=nil>

I hope that helps. Let me know if you need more information. Thanks!

The ruby version is

ruby --version
ruby 2.1.5p273 (2014-11-13 revision 48405) [x86_64-darwin13.0]

@jonatack
Copy link
Contributor

What is REDACTED, please?

@jonatack
Copy link
Contributor

Do you have a failing spec I can use to reproduce this?

@jaredbeck
Copy link
Contributor Author

What is REDACTED, please?

To redact is to censor or obscure (part of a text) for legal or security concerns. But, you probably knew that :)

Do you have a failing spec I can use to reproduce this?

No, sorry, but I'm happy to give it a shot. I don't see a spec file for adapters/active_record/ransack/nodes/condition.rb, and casted_array_with_in_predicate? is a private method. Where would you put a test like this? In spec/ransack/adapters/active_record/base_spec.rb? Thanks!

@jonatack
Copy link
Contributor

@jaredbeck I don't have enough information to reproduce your issue. You have shown me a basic search query, which works and all Ransack specs pass, so your issue is coming from something else. I don't know what your REDACTED constant or object is, and it is likely what is causing the problem. We need either that, or a search spec from you to reproduce the issue (it doesn't matter right now where the spec should go, what matters is reproducing your issue).

@jaredbeck
Copy link
Contributor Author

If no one can reproduce it, we cannot solve it and this issue will be closed ..

Woah. I just said I'm happy to do the legwork on a failing spec. I think it's a bit early to shut down on me, man. Please check out my questions about your test suite re: where the failing spec should go.

@jonatack
Copy link
Contributor

search = REDACTED.search(params[:search]).results is not enough information to reproduce this issue. It's coming from something else in your code. Closing this until you find it or provide the info.

@jaredbeck
Copy link
Contributor Author

OK, no worries, I'll keep working on something more reproducible. Thanks.

@jonatack
Copy link
Contributor

Sure. I'm happy to have another look if we can reproduce the issue.

@jaredbeck
Copy link
Contributor Author

It could be that the Condition <attributes: ["with_unit"], predicate: in, values: ["something"]> is the problem. This condition comes from a param like "with_unit_in"=>"something". Naturally, the IN clause expects something ennumerable, not a string. If I change the param to "with_unit_in"=>[], my test passes.

Do you want ransack to be responsible for raising a helpful error when a string is provided to an IN clause? I think we could provide a more helpful error than the current NoMethodError. Maybe such a check could live in Ransack::Nodes::Condition. Would you be interested in a PR for that?

@jaredbeck
Copy link
Contributor Author

Alternatively, a helpful check like that could be the responsibility of Arel, but that's a low-level library and they might not want to be responsible for validating inputs (read: idiot-proofing) :)

@jonatack
Copy link
Contributor

You may be right. A regression test to validate that hypothesis (and a helpful error message as a bonus) would be great 👍

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

2 participants