简体   繁体   中英

gets error when using try, but works fine without try

I have this line in my code:

student.user.try(:services).try( :where, {course_id: params[:current_course]} ).try(:first)

It gives the following error:

undefined method `where' for #<Array:0x007fc35070da30>

But when I use it without try like this:

student.user.try(:services).where("course_id = ?", params[:current_course] ).try(:first)

everything is OK.

What's the problem with try?

Per your request adding the backtrace:

activesupport (3.2.8) lib/active_support/core_ext/object/try.rb:36:in `try'
  activerecord (3.2.8) lib/active_record/associations/collection_proxy.rb:89:in `method_missing'
  app/controllers/trainings_controller.rb:926:in `block in transfer_students'
  app/controllers/trainings_controller.rb:922:in `each'
  app/controllers/trainings_controller.rb:922:in `transfer_students'
  actionpack (3.2.8) lib/action_controller/metal/implicit_render.rb:4:in `send_action'
  actionpack (3.2.8) lib/abstract_controller/base.rb:167:in `process_action'
  actionpack (3.2.8) lib/action_controller/metal/rendering.rb:10:in `process_action'
  actionpack (3.2.8) lib/abstract_controller/callbacks.rb:18:in `block in process_action'
  activesupport (3.2.8) lib/active_support/callbacks.rb:491:in `_run__1196091745092505907__process_action__2696008159292751787__callbacks'
  activesupport (3.2.8) lib/active_support/callbacks.rb:405:in `__run_callback'
  activesupport (3.2.8) lib/active_support/callbacks.rb:385:in `_run_process_action_callbacks'
  activesupport (3.2.8) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (3.2.8) lib/abstract_controller/callbacks.rb:17:in `process_action'
  actionpack (3.2.8) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:30:in `block in process_action'
  activesupport (3.2.8) lib/active_support/notifications.rb:123:in `block in instrument'
  activesupport (3.2.8) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (3.2.8) lib/active_support/notifications.rb:123:in `instrument'
  actionpack (3.2.8) lib/action_controller/metal/instrumentation.rb:29:in `process_action'
  actionpack (3.2.8) lib/action_controller/metal/params_wrapper.rb:207:in `process_action'
  activerecord (3.2.8) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (3.2.8) lib/abstract_controller/base.rb:121:in `process'
  actionpack (3.2.8) lib/abstract_controller/rendering.rb:45:in `process'
  actionpack (3.2.8) lib/action_controller/metal.rb:203:in `dispatch'
  actionpack (3.2.8) lib/action_controller/metal/rack_delegation.rb:14:in `dispatch'

Moving this to an answer since it seemed to be helpful.

Reading the release notes, this seems to be the legacy behavior of Object.try . It was changed to the current expectation in Rails 4:

"Object#try will now return nil instead of raise a NoMethodError if the receiving object does not implement the method"

Rails 4.0 Release notes

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM