searchlogic icon indicating copy to clipboard operation
searchlogic copied to clipboard

undefined method `to_i'

Open ryanwood opened this issue 14 years ago • 27 comments

After a recent upgrade of the searchlogic gem, one of our forms (using formtastic) broke. I narrowed down the problem to gem version 2.4.15 and after. Version 2.4.14 doesn't cause any problems. IT errors out trying to render a checkbox. We are not using searchlogic at all in this form, but the mere inclusion of the gem causes the problem. Any ideas?

The error message is below:

NoMethodError in Users#show

Showing app/views/users/edit.haml where line #39 raised:

undefined method `to_i' for #<Class:0x36ba690>

Extracted source (around line #39):

36:           = v.input :facebook_fan_page_url, :label => 'Facebook Fan Page', :required => false
37: 
38:           - v.inputs :name => "Credit Cards Accepted", :class => "cards" do
39:             = v.input :accepts_visa, :label => image_tag("payment/visa.gif", :alt => "Visa") , :required => false
40:             = v.input :accepts_master_card, :label => image_tag("payment/master_card.gif", :alt => "Master Card"), :required => false
41:             = v.input :accepts_american_express, :label => image_tag("payment/american_express.gif", :alt => "American Express"), :required => false
42:             = v.input :accepts_discover, :label => image_tag("payment/discover.gif", :alt => "Discover"), :required => false

RAILS_ROOT: /Users/ryanwood/code/my-app/my-app.com
Application Trace | Framework Trace | Full Trace

/opt/local/lib/ruby/gems/1.8/gems/searchlogic-2.4.15/lib/searchlogic/named_scopes/conditions.rb:88:in `method_missing'
/opt/local/lib/ruby/gems/1.8/gems/searchlogic-2.4.15/lib/searchlogic/named_scopes/association_conditions.rb:19:in `method_missing'
/opt/local/lib/ruby/gems/1.8/gems/searchlogic-2.4.15/lib/searchlogic/named_scopes/association_ordering.rb:27:in `method_missing'
/opt/local/lib/ruby/gems/1.8/gems/searchlogic-2.4.15/lib/searchlogic/named_scopes/ordering.rb:30:in `method_missing'
/opt/local/lib/ruby/gems/1.8/gems/searchlogic-2.4.15/lib/searchlogic/named_scopes/or_conditions.rb:28:in `method_missing'
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1959:in `method_missing_without_paginate'
/Users/ryanwood/.gem/ruby/1.8/gems/will_paginate-2.3.11/lib/will_paginate/finder.rb:170:in `method_missing'
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/named_scope.rb:181:in `send'
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/named_scope.rb:181:in `method_missing'
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2143:in `with_scope'
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/named_scope.rb:113:in `__send__'
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/named_scope.rb:113:in `with_scope'
/opt/local/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/named_scope.rb:174:in `method_missing'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:862:in `check_box_checked?'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:794:in `to_check_box_tag'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:686:in `check_box'
/opt/local/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:995:in `check_box'
/opt/local/lib/ruby/gems/1.8/gems/formtastic-0.9.7/lib/formtastic.rb:1109:in `boolean_input'
/opt/local/lib/ruby/gems/1.8/gems/formtastic-0.9.7/lib/formtastic.rb:1121:in `send'
/opt/local/lib/ruby/gems/1.8/gems/formtastic-0.9.7/lib/formtastic.rb:1121:in `inline_input_for'
/opt/local/lib/ruby/gems/1.8/gems/formtastic-0.9.7/lib/formtastic.rb:97:in `send'
/opt/local/lib/ruby/gems/1.8/gems/formtastic-0.9.7/lib/formtastic.rb:97:in `input'
/opt/local/lib/ruby/gems/1.8/gems/formtastic-0.9.7/lib/formtastic.rb:96:in `map'
/opt/local/lib/ruby/gems/1.8/gems/formtastic-0.9.7/lib/formtastic.rb:96:in `input'
/Users/ryanwood/code/my-app/my-app.com/app/views/users/edit.haml:39:in `_run_haml_app47views47users47edit46haml'
/Users/ryanwood/code/my-app/my-app.com/app/views/users/edit.haml:38:in `_run_haml_app47views47users47edit46haml'
/Users/ryanwood/code/my-app/my-app.com/app/views/users/edit.haml:16:in `_run_haml_app47views47users47edit46haml'
/Users/ryanwood/code/my-app/my-app.com/app/views/users/edit.haml:5:in `_run_haml_app47views47users47edit46haml'
/Users/ryanwood/code/my-app/my-app.com/app/views/users/edit.haml:4:in `_run_haml_app47views47users47edit46haml'
/Users/ryanwood/code/my-app/my-app.com/app/controllers/users_controller.rb:90:in `edit'
/Users/ryanwood/code/my-app/my-app.com/app/controllers/users_controller.rb:85:in `show'

ryanwood avatar Apr 19 '10 17:04 ryanwood

Can you show your controller method as well?

beaucollins avatar Apr 19 '10 20:04 beaucollins

Nothing to it:

def edit
  @user = current_user
  render :action => "edit"
end

ryanwood avatar Apr 19 '10 20:04 ryanwood

Very similar problem - upgraded my gems and now pages are not working undefined method `to_i' for #Class:0x2b2e62ed2108 Extracted source (around line <%= f.check_box :on_grid %>):

14:     <%= f.label :on_grid %> 
15:     <%= f.check_box :on_grid %>
16:   </p>
17:   <p>
18:     <%= f.label :bleed, "Bleed (mm)" %> 

/usr/lib/ruby/gems/1.8/gems/searchlogic-2.4.19/lib/searchlogic/named_scopes/conditions.rb:81:in `method_missing'
/usr/lib/ruby/gems/1.8/gems/searchlogic-2.4.19/lib/searchlogic/named_scopes/association_conditions.rb:19:in `method_missing'
/usr/lib/ruby/gems/1.8/gems/searchlogic-2.4.19/lib/searchlogic/named_scopes/association_ordering.rb:27:in `method_missing'
/usr/lib/ruby/gems/1.8/gems/searchlogic-2.4.19/lib/searchlogic/named_scopes/ordering.rb:30:in `method_missing'
/usr/lib/ruby/gems/1.8/gems/searchlogic-2.4.19/lib/searchlogic/named_scopes/or_conditions.rb:28:in `method_missing'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:1959:in `method_missing_without_paginate'
/usr/lib/ruby/gems/1.8/gems/mislav-will_paginate-2.3.11/lib/will_paginate/finder.rb:170:in `method_missing'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/named_scope.rb:181:in `send'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/named_scope.rb:181:in `method_missing'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/base.rb:2143:in `with_scope'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/named_scope.rb:113:in `__send__'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/named_scope.rb:113:in `with_scope'
/usr/lib/ruby/gems/1.8/gems/activerecord-2.3.5/lib/active_record/named_scope.rb:174:in `method_missing'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:862:in `check_box_checked?'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:794:in `to_check_box_tag'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:686:in `check_box'
/usr/lib/ruby/gems/1.8/gems/actionpack-2.3.5/lib/action_view/helpers/form_helper.rb:995:in `check_box'
/var/www/html/renaissance/app/views/page_assemblies/_form.html.erb:15:in `_run_erb_app47views47page_assemblies47_form46html46erb_locals_f_form_object'
/var/www/html/renaissance/app/views/jobs/_page_assembly.html.erb:5:in `_run_erb_app47views47jobs47_page_assembly46html46erb_locals_object_page_assembly'
/var/www/html/renaissance/app/views/jobs/_page_assembly.html.erb:4:in `_run_erb_app47views47jobs47_page_assembly46html46erb_locals_object_page_assembly'
/var/www/html/renaissance/app/views/jobs/edit.html.erb:36:in `_run_erb_app47views47jobs47edit46html46erb'
/var/www/html/renaissance/app/views/jobs/edit.html.erb:34:in `each'
/var/www/html/renaissance/app/views/jobs/edit.html.erb:34:in `_run_erb_app47views47jobs47edit46html46erb'

controller is equally as simple as Ryan's

benry62 avatar Apr 22 '10 15:04 benry62

Strange, I wasn't able to reproduce. My hunch was that the form was binding to an instance of the Class of your model instead of an instantiated model, but I was never able to get it to throw that error. Do either of you have your codebase on github?

beaucollins avatar Apr 22 '10 15:04 beaucollins

My code is in a private repo and I'm unable to share it. Here's my form declaration on the page (using formtastic):

  - semantic_form_for @user, :url => account_path, :html => { :multipart => true, :id => "account" } do |f|

It is binding to an instance of the user class.

ryanwood avatar Apr 22 '10 15:04 ryanwood

Sorry - no. Is there a big difference in 2.1.14 and 2.1.19? I am not quite sure what version I was running before I did a gem update - looking at yr release dates almost certainly it was 2.1.14.

benry62 avatar Apr 22 '10 15:04 benry62

Try this codebase, it's using the gems I saw in ryanwood's backtrace:

http://github.com/beaucollins/searchlogic_regression

A single model User with a :boolean field being rendered as a checkbox with formtastic. Seems to be essence of your issue.

Fire it up and go to /users and add/edit/delete one, see if it throws your error.

beaucollins avatar Apr 22 '10 16:04 beaucollins

Just to confirm - have gone back and re-installed 2.1.14 and everything is working fine config.gem "searchlogic", :version => '2.4.14'

benry62 avatar Apr 22 '10 16:04 benry62

I've been having this problem today, too. So far my digging has revealed that, the place where everything goes wrong is in:

SearchLogic::ActiveRecord::AssociationProxy#send_with_searchlogic()

In there, proxy_reflection.klass.condition?(method) is returning true (in turn because SearchLogic::NamedScopes::Conditions#boolean_condition?(method) is returning true). So at that point, it's sending to the proxy_reflection's class, rather than the instance.

In our case, the code that's instantiating the model (through a has_one association) is along the lines of:

@muffin = current_company.standard_issue_muffin

If I change it to:

@muffin = Muffin.find(current_company.standard_issue_muffin.id)

which avoids the instance being associated with the association. Not an elegant workaround. A proper fix would be awesome. :-)

mathie avatar Apr 23 '10 15:04 mathie

Based upon the assertion that 2.4.14 didn't have the bug, I've taken a look at the changes between it at 2.4.15. I'm betting it's this commit df45eca5530b3fc5ab2e8021d23a6561bd3ecabf that introduced the problem.

mathie avatar Apr 23 '10 15:04 mathie

... however, reverting that commit isn't enough to make it work. Ah well, back to the drawing board. :-)

mathie avatar Apr 23 '10 15:04 mathie

I've encountered what appears to be the same issue using accepts_nested_attributes_for. I've created a sample rails app and if you start it up and go to http://localhost:3000/orders/new you'll see the problem.

The sample rails app is at http://github.com/mhaley/searchlogicbug

mhaley avatar May 01 '10 01:05 mhaley

I can confirm this bug. The RoR app that I'm working with relies heavily on accepts_nested_attributes_for. I'm currently working with a nested model form, and just spent 4 hours trying to figure out why a nested checkbox wouldn't work... I found this posting, uninstalled searchlogic, and like magic, my nested checkbox is working correctly.

ghost avatar May 30 '10 10:05 ghost

I've been bitten by this bug too.

tjoneseng avatar Jun 04 '10 19:06 tjoneseng

Note, this isn't a bug to do with forms and checkboxes, per se. It's to do with accessing boolean attributes via associations. See #84.

h-lame avatar Jun 08 '10 16:06 h-lame

I got this problem too.

dombesz avatar Jun 17 '10 08:06 dombesz

Another failing test case: http://github.com/jdfrens/searchlogicspike

sjain avatar Jun 29 '10 21:06 sjain

i've been bitten as well

danhixon avatar Jul 01 '10 15:07 danhixon

I have had this problem as well. Reverting to 2.4.14 fixed it for me.

joerichsen avatar Jul 12 '10 08:07 joerichsen

I also had the same problem and had to revert back to 2.4.14

shingi avatar Jul 23 '10 23:07 shingi

Add me to the count of people being bitten by this...

gamov avatar Jul 28 '10 04:07 gamov

Another one here - back to 2.4.14 for now...

elandesign avatar Aug 19 '10 07:08 elandesign

Yeah, experienced this too with version 2.4.23.

Dahie avatar Sep 20 '10 11:09 Dahie

still open with latest git, #99 related

masterkain avatar Nov 27 '10 14:11 masterkain

still having this problem in version 2.4.27. Having problem with models that accepted as nested_attributes

jandjshi avatar Jan 25 '11 23:01 jandjshi

same here, 2.4.27

dlupu avatar Jan 31 '11 15:01 dlupu

Saw this bug, same solution: use 2.4.14!

DCarper avatar Apr 13 '11 15:04 DCarper