2

I'm trying to integrate gem called slim_scrooge in my app but I keep getting some weird issue i.e getting the following error

NoMethodError: undefined method reverse' for nil:NilClass from /Users/ratatouille/.rvm/gems/ruby-1.9.3-p547@minerva/gems/activerecord-3.2.10/lib/active_record/connection_adapters/abstract/database_statements.rb:11:inblock in to_sql'

Upon careful debugging I found that the error is getting produce on .to_sql on arel.

To find it why I added the breakpoint on this line

and then I found this

## On First run

Performer.find(1985)

 From: /Users/Ratatouille/.rvm/gems/ruby-1.9.3-p547@minerva/gems/activerecord-3.2.10/lib/active_record/relation.rb @ line 171 ActiveRecord::Relation#exec_queries:

    166: 
    167:       default_scoped = with_default_scope
    168: 
    169:       if default_scoped.equal?(self)
    170:         @records = if @readonly_value.nil? && !@klass.locking_enabled?
 => 171:           binding.pry
    172:           eager_loading? ? find_with_associations : @klass.find_by_sql(arel, @bind_values)
    173:         else
    174:           IdentityMap.without do
    175:             eager_loading? ? find_with_associations : @klass.find_by_sql(arel, @bind_values)
    176:           end

[1] pry(#<ActiveRecord::Relation>)> arel.to_sql
=> "SELECT  `performers`.* FROM `performers`  WHERE `performers`.`id` = ? LIMIT 1"

## Second Run for same record

Performer.find(1985)

From: /Users/ratatouille/.rvm/gems/ruby-1.9.3-p547@minerva/gems/activerecord-3.2.10/lib/active_record/relation.rb @ line 171 ActiveRecord::Relation#exec_queries:

    166: 
    167:       default_scoped = with_default_scope
    168: 
    169:       if default_scoped.equal?(self)
    170:         @records = if @readonly_value.nil? && !@klass.locking_enabled?
 => 171:           binding.pry
    172:           eager_loading? ? find_with_associations : @klass.find_by_sql(arel, @bind_values)
    173:         else
    174:           IdentityMap.without do
    175:             eager_loading? ? find_with_associations : @klass.find_by_sql(arel, @bind_values)
    176:           end

[1] pry(#<ActiveRecord::Relation>)> arel.to_sql
NoMethodError: undefined method `reverse' for nil:NilClass
from /Users/ratatouille/.rvm/gems/ruby-1.9.3-p547@minerva/gems/activerecord-3.2.10/lib/active_record/connection_adapters/abstract/database_statements.rb:11:in `block in to_sql'

I'm going Nut over the fact that why is the 2nd time the arel.to_sql doesn't work

Any one has a clue

More info -

  • Rails 3.2.10
Cœur
  • 37,241
  • 25
  • 195
  • 267
Ratatouille
  • 1,372
  • 5
  • 23
  • 50

1 Answers1

0

Seems this is a bug with gem. You can see the discussion here.

There are two possible ways to solve it.

  1. If you are using query_reviewer gem try disabling it.

  2. If the above step doesn't work. Fix the active_record bug by yourself.

    ## rails/activerecord/lib/active_record/connection_adapters/abstract/database_statements.rb
    
    def to_sql(arel, binds = [])
      if arel.respond_to?(:ast)
        visitor.accept(arel.ast) do
          quote(*binds.shift.reverse) # root cause for the bug
        end
      else
       arel
      end
    end
    

    Default parameter for binds variable is wrong here. Change it like

    def to_sql(arel, binds = [[]]) # Added an empty array
    

    Check here for how to override rails module methods(easy way). Or you can deploy your gems to localfolder and override them(hard way) check answer for example.

FYI: The error in your post points to database_statements.rb:11 but I find out the source to have the code at line 8. Couldn't find the exact version of yours.

Community
  • 1
  • 1
Siva
  • 7,780
  • 6
  • 47
  • 54
  • Firstly apology for late reply and I extremely sorry but I was looking for a answer as to why the above errors occur the second time what you just listed above is something I already knew – Ratatouille Sep 26 '14 at 08:23
  • also I dont seem to follow why you mention this `def to_sql(arel, binds = [[]])` since when as mention in code section the `@bind_values` (sorry If it was not clear) is always passed to `sql` so their never a case of default value been taken – Ratatouille Sep 26 '14 at 08:27
  • Anyway I will still award you the bounty since I don't see anyone answering this question in less then 2 hrs before the bounty end and it better to award rather then getting wasted since I'm not getting the point back to my account anyway but I'm still not accepting the answer because of the reason explained above don't take me wrong on this – Ratatouille Sep 26 '14 at 08:32
  • @Ratatouille I will not take that for granted. Is it possible for you to create a git repo of this project ? Or at least a gist(including Gemfile) to reproduce this error – Siva Sep 26 '14 at 12:32
  • you can try it with any app just include `slim_scrooge`gem in it your `Gemfile` – Ratatouille Sep 26 '14 at 16:39
  • @shiva : How can i add the above code in my application to get it working ? . As there is no `activerecord` folder in my `rails` folder, where i should put the code given in the answer ? – huzefa biyawarwala Aug 04 '16 at 06:54