[英]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
.阅读发行说明,这似乎是
Object.try
的遗留行为。 It was changed to the current expectation in Rails 4:在 Rails 4 中它被更改为当前的期望:
"Object#try will now return nil instead of raise a NoMethodError if the receiving object does not implement the method"
“如果接收 object 未实现该方法,Object#try 现在将返回 nil 而不是引发 NoMethodError”
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.