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

Cannot visit Range #32

Closed
polimorfico opened this issue Oct 17, 2011 · 11 comments
Closed

Cannot visit Range #32

polimorfico opened this issue Oct 17, 2011 · 11 comments

Comments

@polimorfico
Copy link

Hi,

I'm trying to use a custom predicate, but I get always the same error.

Ransack.configure do |config|
config.add_predicate 'btw',
:arel_predicate => 'in',
:formatter => proc {|v| Range.new(Date.today, Date.today+1)}
end

Thanks,
Carlos

@ernie
Copy link
Contributor

ernie commented Oct 20, 2011

Can you provide a stack trace?

@dima4p
Copy link

dima4p commented Nov 10, 2011

arel (2.2.1) lib/arel/visitors/visitor.rb:25:in rescue in visit' arel (2.2.1) lib/arel/visitors/visitor.rb:19:invisit'
arel (2.2.1) lib/arel/visitors/to_sql.rb:418:in block in visit_Array' arel (2.2.1) lib/arel/visitors/to_sql.rb:418:inmap'
arel (2.2.1) lib/arel/visitors/to_sql.rb:418:in visit_Array' arel (2.2.1) lib/arel/visitors/visitor.rb:19:invisit'
arel (2.2.1) lib/arel/visitors/to_sql.rb:324:in visit_Arel_Nodes_In' arel (2.2.1) lib/arel/visitors/visitor.rb:19:invisit'
arel (2.2.1) lib/arel/visitors/to_sql.rb:332:in block in visit_Arel_Nodes_And' arel (2.2.1) lib/arel/visitors/to_sql.rb:332:inmap'
arel (2.2.1) lib/arel/visitors/to_sql.rb:332:in visit_Arel_Nodes_And' arel (2.2.1) lib/arel/visitors/visitor.rb:19:invisit'
arel (2.2.1) lib/arel/visitors/to_sql.rb:209:in visit_Arel_Nodes_Grouping' arel (2.2.1) lib/arel/visitors/visitor.rb:19:invisit'
arel (2.2.1) lib/arel/visitors/to_sql.rb:209:in visit_Arel_Nodes_Grouping' arel (2.2.1) lib/arel/visitors/visitor.rb:19:invisit'
arel (2.2.1) lib/arel/visitors/to_sql.rb:144:in block in visit_Arel_Nodes_SelectCore' arel (2.2.1) lib/arel/visitors/to_sql.rb:144:inmap'
arel (2.2.1) lib/arel/visitors/to_sql.rb:144:in visit_Arel_Nodes_SelectCore' arel (2.2.1) lib/arel/visitors/mysql.rb:45:invisit_Arel_Nodes_SelectCore'
arel (2.2.1) lib/arel/visitors/to_sql.rb:129:in block in visit_Arel_Nodes_SelectStatement' arel (2.2.1) lib/arel/visitors/to_sql.rb:129:inmap'
arel (2.2.1) lib/arel/visitors/to_sql.rb:129:in visit_Arel_Nodes_SelectStatement' arel (2.2.1) lib/arel/visitors/mysql.rb:40:invisit_Arel_Nodes_SelectStatement'
arel (2.2.1) lib/arel/visitors/visitor.rb:19:in visit' arel (2.2.1) lib/arel/visitors/visitor.rb:5:inaccept'
arel (2.2.1) lib/arel/visitors/to_sql.rb:18:in block in accept' activerecord (3.1.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:185:inwith_connection'
arel (2.2.1) lib/arel/visitors/to_sql.rb:16:in accept' activerecord (3.1.1) lib/active_record/connection_adapters/abstract/database_statements.rb:9:into_sql'
activerecord (3.1.1) lib/active_record/relation.rb:397:in to_sql' app/controllers/tours_controller.rb:19:inindex'

@fleetio
Copy link

fleetio commented Feb 14, 2012

I'm having the same issue. Trying to generate a BETWEEN query for two dates by using the "in" arel predicate and passing a Range.

Did you ever figure anything out with this? Thanks!

@fleetio
Copy link

fleetio commented Feb 14, 2012

The problem seems to be that by using the in Arel predicate, Ransack sets an internal variable called wants_array to true automatically.

When it actually calls Arel to build the query, the Range is wrapped in an Array which screws up the ability to generate a date range query.

@ernie
Copy link
Contributor

ernie commented Feb 14, 2012

Hey guys, patches/specs appreciated on this one. Or, better yet, see https://twitter.com/erniemiller/status/167289641295294464 and nominate a possible maintainer? :)

@fleetio
Copy link

fleetio commented Feb 15, 2012

I'll do my best to get a patch & spec to allow for date ranges to work properly.

However, I wouldn't be a good candidate to maintain this gem as it's a little out of my skill range. I don't think I'd be able to do it justice. You've set a nice, high bar with this one ;) Really great gem.

@ricsrock
Copy link

ricsrock commented Jun 2, 2012

Anybody found a fix or workaround for this?

@0xCCD
Copy link

0xCCD commented Dec 14, 2012

Same problem here, need to generate a custom predicate which producses a BETWEEN for two values, anybody has a hint how to solve the problem

@pavlitsky
Copy link

Same problem. Ended up with ugly but working solution. Using the code provided by issue starter:

module Ransack
  class Predicate
    attr_accessor :wants_array
  end
end

Ransack.configure do |config|
config.add_predicate 'btw', 
  :arel_predicate => 'in',
  :formatter => proc {|v| Range.new(Date.today, Date.today+1)}
end

Ransack::predicates['btw'].wants_array = false

I.e. just forcing "wants_array" to "false" after creating predicate.

@keichan34
Copy link

@pavlitsky 's hack works, but it'd be nice to have a way to do this without such a hack...

@Pavling
Copy link
Contributor

Pavling commented Mar 19, 2015

Thanks for the hints. Here's a slightly terser way (without the monkey patch of the :wants_array attribute:

Ransack.configure do |config|
  config.add_predicate 'btw', 
    :arel_predicate => 'in',
    :formatter => proc {|v| Range.new(Date.today, Date.today+1)}

  Ransack::predicates['btw'].instance_variable_set('@wants_array', false)
end

I'm just working on a PR to allow the same functionality to be achieved by passing :wants_array => false in the options, like this:

Ransack.configure do |config|
  config.add_predicate 'btw', 
    :wants_array => false,
    :arel_predicate => 'in',
    :formatter => proc {|v| Range.new(Date.today, Date.today+1)}
end

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

9 participants