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

Unchecked checkbox in Rails 4: undefined method `name' for nil:NilClass #400

Closed
exchgr opened this issue Jul 28, 2014 · 16 comments
Closed

Comments

@exchgr
Copy link

exchgr commented Jul 28, 2014

Inside a search_form_for, I have f.check_box :some_setting_true. The checkbox seems to influence searches correctly, but when it's unchecked, it returns:

    undefined method `name' for nil:NilClass

After some digging, the issue seems to be here: https://github.com/activerecord-hackery/ransack/blob/master/lib/ransack/nodes/condition.rb#L146, and occurs due to Condition's attributes being nil when the checkbox is unchecked.

@jonatack
Copy link
Contributor

Hi @exchgr, for handling boolean results in searches, I've found I usually need three states: true, false, and all (default).

In that case, assuming your checkbox field has a checked value of 1 and an unchecked value of 0, the following in a search_form_for would work, using a not_eq predicate:

  <%= f.select :field_not_eq, [['True', 0], ['False', 1], ['All', 2]] %>

Hope this is helpful.

@exchgr
Copy link
Author

exchgr commented Jul 30, 2014

I just tried that, and I still got the same error.

@jonatack
Copy link
Contributor

Hm. I use that code in production. Which version of Ransack/Ruby/Rails are you using? More information to reproduce the issue would be helpful.

@exchgr
Copy link
Author

exchgr commented Jul 30, 2014

Ransack: 1.2.3 (Rails 4 branch)
Ruby: 1.9.3-p484
Rails: 4.0.8

@alexisraca
Copy link

Same problem here but it happens on any field

@milankubin
Copy link

Same problem. search_form -> attribute_fields -> attribute_select works, but i can't seem to create any form field from search_form . Gives me the same error.
Rails 4.2.4 , Ruby 2.2.3, ransack 4.2 branch

@jonatack
Copy link
Contributor

jonatack commented Dec 3, 2015

Thanks for the input. The non-master branches are not maintained. At the time they were for testing updating Ransack for compatibility with not-yet-released versions of Rails. It's better to use the latest release or master. Please chime in if you're having this issue using one of those, with all the info needed to reproduce 👍

@milankubin
Copy link

Unfortunately I meant to say I was getting this with the current version as well.
I will probably dive deeper in this myself, as it's kind of business critical atm, and have decided to move over to 4.2.4 some time ago.
I didn't have this error in the 4.1-stable branch with Rails 4.1.8
I'm going to do another test with master tonight, maybe things have changed for the better, but I guess this information could be useful .

Some code

# app/controller/leads_controller.php
  def index
    leads

    @search = @leads.ransack(params[:q])
    @search.sorts = 'requested_at desc' if @search.sorts.empty?  

    @leads = @search.result.page(params[:page]).per(30)
    @search.build_condition if @search.conditions.empty?
    @leads = LeadDecorator.decorate_collection(@leads)
  end

private
  def leads
    @leads = current_organization.leads
  end
# app/views/leads/index.html.haml
  = render partial: "shared/dynamic_search_form", locals: { s: @search }
# app/views/shared/_dynamic_search_form.html.haml
     = search_form_for s, url: search_leads_path, method: :post do |f|
     [...]
              = f.search_field :name_first_cont

Full trace

Showing /app/views/shared/_dynamic_search_form.html.haml where line #18 raised:

undefined method `name' for nil:NilClass
Extracted source (around line #18):
                  %i
            .box-content.no-border
              = f.search_field :name_first_cont
              = f.condition_fields do |c|
                = render partial: "shared/condition_fields", locals: { f: c }
              %p= link_to_add_fields "Filter toevoegen", f, :condition

Trace of template inclusion: app/views/leads/index.html.haml

Application Trace | Framework Trace | Full Trace
ransack (1.7.0) lib/ransack/nodes/condition.rb:153:in `key'
ransack (1.7.0) lib/ransack/nodes/grouping.rb:52:in `block in []'
ransack (1.7.0) lib/ransack/nodes/grouping.rb:52:in `each'
ransack (1.7.0) lib/ransack/nodes/grouping.rb:52:in `detect'
ransack (1.7.0) lib/ransack/nodes/grouping.rb:52:in `[]'
ransack (1.7.0) lib/ransack/nodes/grouping.rb:186:in `read_attribute'
ransack (1.7.0) lib/ransack/nodes/grouping.rb:122:in `method_missing'
ransack (1.7.0) lib/ransack/search.rb:97:in `method_missing'
ransack (1.7.0) lib/ransack/helpers/form_builder.rb:10:in `value'
actionview (4.2.4) lib/action_view/helpers/tags/base.rb:38:in `value_before_type_cast'
actionview (4.2.4) lib/action_view/helpers/tags/text_field.rb:13:in `block in render'
actionview (4.2.4) lib/action_view/helpers/tags/text_field.rb:13:in `fetch'
actionview (4.2.4) lib/action_view/helpers/tags/text_field.rb:13:in `render'
actionview (4.2.4) lib/action_view/helpers/tags/search_field.rb:6:in `render'
actionview (4.2.4) lib/action_view/helpers/form_helper.rb:998:in `search_field'
actionview (4.2.4) lib/action_view/helpers/form_helper.rb:1323:in `search_field'
haml (4.0.7) lib/haml/helpers/action_view_mods.rb:137:in `call'
haml (4.0.7) lib/haml/helpers/action_view_mods.rb:137:in `block (2 levels) in form_for_with_haml'
haml (4.0.7) lib/haml/helpers.rb:278:in `with_tabs'
haml (4.0.7) lib/haml/helpers/action_view_mods.rb:137:in `block in form_for_with_haml'
actionview (4.2.4) lib/action_view/helpers/capture_helper.rb:38:in `block in capture'
actionview (4.2.4) lib/action_view/helpers/capture_helper.rb:202:in `with_output_buffer'
haml (4.0.7) lib/haml/helpers/action_view_xss_mods.rb:5:in `with_output_buffer_with_haml_xss'
actionview (4.2.4) lib/action_view/helpers/capture_helper.rb:38:in `capture'
haml (4.0.7) lib/haml/helpers/action_view_mods.rb:52:in `capture_with_haml'
actionview (4.2.4) lib/action_view/helpers/form_helper.rb:444:in `form_for'
haml (4.0.7) lib/haml/helpers/action_view_mods.rb:139:in `form_for_with_haml'
haml (4.0.7) lib/haml/helpers/action_view_xss_mods.rb:28:in `form_for_with_haml_xss'
ransack (1.7.0) lib/ransack/helpers/form_helper.rb:34:in `search_form_for'
actionview (4.2.4) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.4) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.4) lib/action_view/template.rb:143:in `render'
actionview (4.2.4) lib/action_view/renderer/partial_renderer.rb:339:in `render_partial'
actionview (4.2.4) lib/action_view/renderer/partial_renderer.rb:310:in `block in render'
actionview (4.2.4) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.4) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.4) lib/action_view/renderer/partial_renderer.rb:309:in `render'
actionview (4.2.4) lib/action_view/renderer/renderer.rb:47:in `render_partial'
actionview (4.2.4) lib/action_view/renderer/renderer.rb:21:in `render'
actionview (4.2.4) lib/action_view/helpers/rendering_helper.rb:32:in `render'
haml (4.0.7) lib/haml/helpers/action_view_mods.rb:10:in `block in render_with_haml'
haml (4.0.7) lib/haml/helpers.rb:89:in `non_haml'
haml (4.0.7) lib/haml/helpers/action_view_mods.rb:10:in `render_with_haml'
actionview (4.2.4) lib/action_view/template.rb:145:in `block in render'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.4) lib/action_view/template.rb:333:in `instrument'
actionview (4.2.4) lib/action_view/template.rb:143:in `render'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:54:in `block (2 levels) in render_template'
actionview (4.2.4) lib/action_view/renderer/abstract_renderer.rb:39:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
actionview (4.2.4) lib/action_view/renderer/abstract_renderer.rb:39:in `instrument'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:53:in `block in render_template'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:61:in `render_with_layout'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:52:in `render_template'
actionview (4.2.4) lib/action_view/renderer/template_renderer.rb:14:in `render'
actionview (4.2.4) lib/action_view/renderer/renderer.rb:42:in `render_template'
actionview (4.2.4) lib/action_view/renderer/renderer.rb:23:in `render'
actionview (4.2.4) lib/action_view/rendering.rb:100:in `_render_template'
actionpack (4.2.4) lib/action_controller/metal/streaming.rb:217:in `_render_template'
actionview (4.2.4) lib/action_view/rendering.rb:83:in `render_to_body'
actionpack (4.2.4) lib/action_controller/metal/rendering.rb:32:in `render_to_body'
actionpack (4.2.4) lib/action_controller/metal/renderers.rb:37:in `render_to_body'
actionpack (4.2.4) lib/abstract_controller/rendering.rb:25:in `render'
actionpack (4.2.4) lib/action_controller/metal/rendering.rb:16:in `render'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:44:in `block (2 levels) in render'
activesupport (4.2.4) lib/active_support/core_ext/benchmark.rb:12:in `block in ms'
/Users/milan/.rbenv/versions/2.2.3/lib/ruby/2.2.0/benchmark.rb:303:in `realtime'
activesupport (4.2.4) lib/active_support/core_ext/benchmark.rb:12:in `ms'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:44:in `block in render'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:87:in `cleanup_view_runtime'
searchkick (1.0.3) lib/searchkick/logging.rb:113:in `cleanup_view_runtime'
activerecord (4.2.4) lib/active_record/railties/controller_runtime.rb:25:in `cleanup_view_runtime'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:43:in `render'
wicked_pdf (1.0.2) lib/wicked_pdf/pdf_helper.rb:23:in `render_with_wicked_pdf'
actionpack (4.2.4) lib/action_controller/metal/implicit_render.rb:10:in `default_render'
actionpack (4.2.4) lib/action_controller/metal/implicit_render.rb:5:in `send_action'
actionpack (4.2.4) lib/abstract_controller/base.rb:198:in `process_action'
actionpack (4.2.4) lib/action_controller/metal/rendering.rb:10:in `process_action'
actionpack (4.2.4) lib/abstract_controller/callbacks.rb:20:in `block in process_action'
activesupport (4.2.4) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.4) lib/active_support/callbacks.rb:117:in `call'
activesupport (4.2.4) lib/active_support/callbacks.rb:555:in `block (2 levels) in compile'
activesupport (4.2.4) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.4) lib/active_support/callbacks.rb:505:in `call'
activesupport (4.2.4) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
activesupport (4.2.4) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
activesupport (4.2.4) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.4) lib/abstract_controller/callbacks.rb:19:in `process_action'
actionpack (4.2.4) lib/action_controller/metal/rescue.rb:29:in `process_action'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `block in instrument'
activesupport (4.2.4) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
activesupport (4.2.4) lib/active_support/notifications.rb:164:in `instrument'
actionpack (4.2.4) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
actionpack (4.2.4) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
searchkick (1.0.3) lib/searchkick/logging.rb:108:in `process_action'
activerecord (4.2.4) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
actionpack (4.2.4) lib/abstract_controller/base.rb:137:in `process'
actionview (4.2.4) lib/action_view/rendering.rb:30:in `process'
actionpack (4.2.4) lib/action_controller/metal.rb:196:in `dispatch'
actionpack (4.2.4) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
actionpack (4.2.4) lib/action_controller/metal.rb:237:in `block in action'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:76:in `call'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:76:in `dispatch'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:45:in `serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:43:in `block in serve'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `each'
actionpack (4.2.4) lib/action_dispatch/journey/router.rb:30:in `serve'
actionpack (4.2.4) lib/action_dispatch/routing/route_set.rb:821:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
apipie-rails (0.3.5) lib/apipie/static_dispatcher.rb:65:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/rack/agent_hooks.rb:30:in `traced_call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/rack/browser_monitoring.rb:32:in `traced_call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
bullet (4.14.10) lib/bullet/rack.rb:12:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
apipie-rails (0.3.5) lib/apipie/extractor/recorder.rb:132:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
meta_request (0.3.4) lib/meta_request/middlewares/app_request_handler.rb:13:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
meta_request (0.3.4) lib/meta_request/middlewares/meta_request_handler.rb:13:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
warden (1.2.3) lib/warden/manager.rb:35:in `block in call'
warden (1.2.3) lib/warden/manager.rb:34:in `catch'
warden (1.2.3) lib/warden/manager.rb:34:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack (1.6.4) lib/rack/etag.rb:24:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack (1.6.4) lib/rack/head.rb:13:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/flash.rb:260:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/cookies.rb:560:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
activerecord (4.2.4) lib/active_record/query_cache.rb:36:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
activerecord (4.2.4) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
activerecord (4.2.4) lib/active_record/migration.rb:377:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
activesupport (4.2.4) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
activesupport (4.2.4) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
activesupport (4.2.4) lib/active_support/callbacks.rb:81:in `run_callbacks'
actionpack (4.2.4) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack-contrib (1.4.0) lib/rack/contrib/response_headers.rb:17:in `call'
meta_request (0.3.4) lib/meta_request/middlewares/headers.rb:16:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
railties (4.2.4) lib/rails/rack/logger.rb:38:in `call_app'
railties (4.2.4) lib/rails/rack/logger.rb:20:in `block in call'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `block in tagged'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:26:in `tagged'
activesupport (4.2.4) lib/active_support/tagged_logging.rb:68:in `tagged'
railties (4.2.4) lib/rails/rack/logger.rb:20:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
request_store (1.2.1) lib/request_store/middleware.rb:8:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/request_id.rb:21:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack (1.6.4) lib/rack/runtime.rb:18:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
activesupport (4.2.4) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
actionpack (4.2.4) lib/action_dispatch/middleware/static.rb:116:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
railties (4.2.4) lib/rails/engine.rb:518:in `call'
railties (4.2.4) lib/rails/application.rb:165:in `call'
newrelic_rpm (3.14.0.305) lib/new_relic/agent/instrumentation/middleware_tracing.rb:78:in `call'
rack (1.6.4) lib/rack/content_length.rb:15:in `call'
thin (1.6.4) lib/thin/connection.rb:86:in `block in pre_process'
thin (1.6.4) lib/thin/connection.rb:84:in `catch'
thin (1.6.4) lib/thin/connection.rb:84:in `pre_process'
thin (1.6.4) lib/thin/connection.rb:53:in `process'
faye-websocket (0.10.2) lib/faye/adapters/thin.rb:40:in `process'
thin (1.6.4) lib/thin/connection.rb:39:in `receive_data'
faye-websocket (0.10.2) lib/faye/adapters/thin.rb:44:in `receive_data'
eventmachine (1.0.8) lib/eventmachine.rb:193:in `run_machine'
eventmachine (1.0.8) lib/eventmachine.rb:193:in `run'
thin (1.6.4) lib/thin/backends/base.rb:73:in `start'
thin (1.6.4) lib/thin/server.rb:162:in `start'
rack (1.6.4) lib/rack/handler/thin.rb:19:in `run'
rack (1.6.4) lib/rack/server.rb:286:in `start'
railties (4.2.4) lib/rails/commands/server.rb:80:in `start'
railties (4.2.4) lib/rails/commands/commands_tasks.rb:80:in `block in server'
railties (4.2.4) lib/rails/commands/commands_tasks.rb:75:in `tap'
railties (4.2.4) lib/rails/commands/commands_tasks.rb:75:in `server'
railties (4.2.4) lib/rails/commands/commands_tasks.rb:39:in `run_command!'
railties (4.2.4) lib/rails/commands.rb:17:in `<top (required)>'
bin/rails:4:in `require'
bin/rails:4:in `<main>'
#  config/initializers/ransack.rb 
Ransack::Adapters::ActiveRecord::Base.class_eval('remove_method :search')

ransack-1.7.0
rails 4.2.4
ruby 2.2.3

Much obliged!

@milankubin
Copy link

Just pulled master, same result :(

@jonatack
Copy link
Contributor

jonatack commented Dec 9, 2015

Hm... it almost looks like the different posters are possibly having different issues... and yet no one has provided enough info to reproduce their issue.

I suspect your issue involves your models or associations, but you didn't show them. I'm not sure the issue is with Ransack but can't tell from what you've provided (please see the Contributing Guide).

What would be helpful would be a self-contained gist like this one or this one, or if you don't like writing test assertions, then perhaps this one 😉

@milankubin
Copy link

Yep. I have many gems loaded. I'm suspecting the same. I got some
complex logic going on and have to trace back to where I was still able to
load search fields.

From the start I have been using a complex initializer
I had found to be able to search by scopes on hstore fields (arrays of
hashes.. Don't get me started.. :)) through ransackers, and everything
worked through that.
I cleaned this up when I knew it would break in 4.2
But now unable to load associations and scopes properly into that select,
I wanted to go around it by adding some extra named fields.
This ended up
being problematic too. Such pain. But I will report back soon with proper
testing and code excerpts!,

@driehuis
Copy link

The top of that stack trace looks familiar. I have not been able to reproduce my issue outside of a Rails view, so I put up a minimal rails app on github that demonstrates my issue. See https://github.com/driehuis/ransack-issue400.

Obviously doing something wrong, but for the life of me can't figure it out. Needless to say, my app is usable without the dropdown select, but getting it to work aould be way cool.

@bplexico
Copy link

bplexico commented Jan 24, 2017

I'm having a similar issue with search_form_for. In the controller when I create the search object in the index view, it looks like this:

Ransack::Search<class: SampleReport, base: Grouping <conditions: [Condition <attributes: [nil], values: [nil]>], combinator: and>>

I would expect this to be the case on the initial load of the index view since I'm not doing any searching at this point but as you can see, in the conditions there is no predicate like there is when you create a search object on the command line, like so:

SampleReport.ransack({org_eq: 'XX'})

Ransack::Search<class: SampleReport, base: Grouping <conditions: [Condition <attributes: ["org"], predicate: eq, values: ["XX"]>], combinator: and>>

It's failing in this method when you reference predicate.name:

def key
        @key ||= attributes.map(&:name).join("_#{combinator}_") +
          "_#{predicate.name}"
end

from: https://github.com/activerecord-hackery/ransack/blob/master/lib/ransack/nodes/condition.rb

Should predicate.name be replaced with predicate_name where you check for the existence of predicate?

def predicate_name
      predicate.name if predicate
end

Edit to add more info:

Calling SampleReport.ransack(nil) from the command line results in:

Ransack::Search<class: RecentProReport, base: Grouping <combinator: and>>

Calling SampleReport.ransack(params[:q]) in the controller where params[:q] == nil results in:

Ransack::Search<class: SampleReport, base: Grouping <conditions: [Condition <attributes: [nil], values: [nil]>], combinator: and>>

@bplexico
Copy link

If it helps anyone else, my problem referenced in the comment above was that build_grouping and build_condition were being called when the params were nil. Once I added a check to make sure they weren't called unless some params were present my issue was resolved.

@nsiefken
Copy link

nsiefken commented Feb 8, 2017

Thanks @bplexico, your solution fixed my problem!

@scarroll32
Copy link
Member

Issues closed as resolved.

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

8 participants