简体   繁体   English

使用 try 时出错,但不使用 try 也能正常工作

[英]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”

Rails 4.0 Release notes Rails 4.0 发行说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 尝试使用邮递员登录时出现Rails路由错误 - Rails routing error when try to login using postman 我尝试生成支架时出现Rails错误 - Rails error when I try generate a scaffold 当我尝试在 rails 6 中使用 select 2 时出错 - Error when i try to use select 2 in rails 6 当我尝试提交没有数据的表单时,它显示带有控制器创建操作视图的验证错误 - When i try to submit form without data, it shows the validation error with controller create action view 当我尝试使用 admin/test_email 测试 email 发送时,Redmine 中的错误 500 - Error 500 in Redmine when I try to test email send using admin/test_email 在没有电子邮件确认的情况下尝试登录时处理 Devise::SessionsController#new 错误中的 ActionController::UnknownFormat (Rails) - Handle ActionController::UnknownFormat in Devise::SessionsController#new error when try to login without E-mail confirmation (Rails) 尝试安装RSpec时为什么会出现此错误? - Why am I getting this error when I try to install RSpec? 当我尝试安装pg gem时导致此错误的原因是什么? - What is causing this error when I try to install the pg gem? S3 :: Error :: SignatureDoesNotMatch:当我尝试保存新对象时 - S3::Error::SignatureDoesNotMatch: when I try and save a new object 当我尝试运行 rails s 时出现此错误 - i have this error when I try to run rails s
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM