Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/62.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ruby on rails rails<;4.0“;试一试;方法投掷指名者?_Ruby On Rails_Rails Activerecord_Nomethoderror - Fatal编程技术网

Ruby on rails rails<;4.0“;试一试;方法投掷指名者?

Ruby on rails rails<;4.0“;试一试;方法投掷指名者?,ruby-on-rails,rails-activerecord,nomethoderror,Ruby On Rails,Rails Activerecord,Nomethoderror,为什么要尝试抛出错误?这不是违背了全部目的吗?也许就在控制台里 ruby-1.9.2-p180 :101 > User.first.try(:something) NoMethodError: undefined method `something' for #<User:0x000001046ad128> from /Users/me/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.10/lib/active_model/

为什么要尝试抛出错误?这不是违背了全部目的吗?也许就在控制台里

ruby-1.9.2-p180 :101 > User.first.try(:something)
NoMethodError: undefined method `something' for #<User:0x000001046ad128>
    from /Users/me/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.10/lib/active_model/attribute_methods.rb:392:in `method_missing'
    from /Users/me/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.10/lib/active_record/attribute_methods.rb:46:in `method_missing'
    from (irb):101
    from /Users/me/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.10/lib/rails/commands/console.rb:44:in `start'
    from /Users/me/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.10/lib/rails/commands/console.rb:8:in `start'
    from /Users/me/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.10/lib/rails/commands.rb:23:in `<top (required)>'
    from script/rails:6:in `require'
    from script/rails:6:in `<main>'
ruby-1.9.2-p180:101>用户。首先。尝试(:某物)
NoMethodError:未定义的方法'something'#
from/Users/me/.rvm/gems/ruby-1.9.2-p180/gems/activemodel-3.0.10/lib/active\u model/attribute\u methods.rb:392:in'method\u missing'
from/Users/me/.rvm/gems/ruby-1.9.2-p180/gems/activerecord-3.0.10/lib/active\u record/attribute\u methods.rb:46:in'method\u missing'
来自(irb):101
from/Users/me/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.10/lib/rails/commands/console.rb:44:in'start'
from/Users/me/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.10/lib/rails/commands/console.rb:8:in'start'
来自/Users/me/.rvm/gems/ruby-1.9.2-p180/gems/railties-3.0.10/lib/rails/commands.rb:23:in`'
来自脚本/rails:6:in'require'
来自脚本/rails:6:in`'
编辑:

谢谢各位,现在我明白了

有没有一种方法可以在不使用
respond\u to?
的情况下执行我想要的操作,例如
User.try(:something)
返回
nil
而不是抛出错误?

Rails 3 您从以下方面误解了
try
的工作原理:

试试(*a和b)
调用symbol方法标识的方法,向其传递任何参数和/或指定的块,就像常规Ruby
对象#send
一样

但是,与该方法不同,如果接收对象是
nil
对象或
NilClass
,则不会引发
NoMethodError
异常,而是返回
nil

以及以下版本:

试试(*args)
nil
上调用
try
总是返回
nil

因此,
try
不会忽略对对象调用不存在的方法的尝试,它会忽略对
nil
调用方法的尝试,并返回
nil
,而不是引发异常。
try
方法只是一种简单的方法,可以避免在方法调用链的每一步都检查
nil


轨道4 在Rails 4中,
try
的行为已经改变,因此现在它:

调用public方法,该方法的名称作为第一个参数,就像
public\u send
一样,除非接收方没有响应它,否则调用将返回
nil
,而不是引发异常

所以现在
try
会同时处理这两个检查。如果您想要Rails 3的行为,有:

try
相同,但如果接收[sic]不是
nil
并且没有实现[sic]尝试的方法,则将引发
NoMethodError
异常

这就是try所做的

调用由symbol方法标识的方法,并将其传递给任何 参数和/或指定的块,就像常规Ruby一样 对象#发送。然而,与该方法不同的是,命名者 如果 接收对象是nil对象或nil类

那么,假设您在控制器中设置了
@user
,但没有实例化它,然后
@user.try(:foo)
=>无
而不是

@user.foo
NoMethodError: undefined method `foo' for nil:NilClass

这里重要的一点是try是一种实例方法。它也是。

我知道这已经很旧了,但它可能会帮助其他人,因为这是我在谷歌上搜索这个问题时出现的第一件事。我“借用”了try的代码,并实现了我自己的try\u方法方法,其作用与try类似,只是它在调用send之前先检查该方法是否存在。我在对象中实现了它,并将其放在初始值设定项中,现在我可以在任何对象上调用它

class Object
  # Invokes the method identified by _method_, passing it any
  # arguments specified, just like the regular Ruby <tt>Object#send</tt> does.
  #
  # *Unlike* that method however, a +NoMethodError+ exception will *not* be raised
  # if the method does not exist.
  #
  # This differs from the regular Ruby <tt>Object#try</tt> method which only
  # suppresses the +NoMethodError+ exception if the object is Nil
  #
  # If try_method is called without a method to call, it will yield any given block with the object.
  #
  # Please also note that +try_method+ is defined on +Object+, therefore it won't work with
  # subclasses of +BasicObject+. For example, using try_method with +SimpleDelegator+ will
  # delegate +try_method+ to target instead of calling it on delegator itself.
  #
  # ==== Examples
  #
  # Without +try_method+
  #   @person && @person.respond_to?(:name) && @person.name
  # or
  #   (@person && @person.respond_to?(:name)) ? @person.name : nil
  #
  # With +try_method+
  #   @person.try_method(:name)
  #
  # +try_method+ also accepts arguments and/or a block, for the method it is trying
  #   Person.try_method(:find, 1)
  #   @people.try_method(:collect) {|p| p.name}
  #
  # Without a method argument try_method will yield to the block unless the receiver is nil.
  #   @person.try_method { |p| "#{p.first_name} #{p.last_name}" }
  #--
  # +try_method+ behaves like +Object#send+, unless called on +NilClass+ or a class that does not implement _method_.
  def try_method(method=nil, *args, &block)
    if method == nil && block_given?
      yield self
    elsif respond_to?(method)
      __send__(method, *args, &block)
    else
      nil
    end
  end
end

class NilClass
  # Calling +try_method+ on +nil+ always returns +nil+.
  # It becomes specially helpful when navigating through associations that may return +nil+.
  #
  # === Examples
  #
  #   nil.try_method(:name) # => nil
  #
  # Without +try_method+
  #   @person && @person.respond_to(:children) && !@person.children.blank? && @person.children.respond_to(:first) && @person.children.first.respond_to(:name) && @person.children.first.name
  #
  # With +try_method+
  #   @person.try_method(:children).try_method(:first).try_method(:name)
  def try_method(*args)
    nil
  end
end
类对象
#调用由_method_标识的方法,并将其传递给任何
#指定参数,就像常规Ruby对象#send一样。
#
#*但是,与*该方法不同,*不会*引发+NoMethodError+异常
#如果该方法不存在。
#
#这与常规Ruby对象#try方法不同,后者只
#如果对象为Nil,则抑制+NoMethodError+异常
#
#如果在没有方法调用的情况下调用try_方法,它将生成包含该对象的任何给定块。
#
#还请注意,+try_method+是在+Object+上定义的,因此它不适用于
#+BasicObject+的子类。例如,将try_方法与+SimpleDelegator+will一起使用
#delegate+尝试将\u method+作为目标,而不是在delegator本身上调用它。
#
#==示例
#
#不使用+try\u方法+
#@person&&@person.response_to?(:name)&&&@person.name
#或
#(@person&&@person.response_to?(:name))@姓名:无
#
#使用+try\u方法+
#@person.try_方法(:name)
#
#+try_method+还接受它正在尝试的方法的参数和/或块
#Person.try_方法(:find,1)
#@people.try_方法(:collect){| p | p.name}
#
#如果没有method参数,try_method将屈服于块,除非接收方为nil。
#@person.try_方法{p |“{p.first_name}{p.last_name}”
#--
#+try_method+的行为类似于+Object#send+,除非在+NilClass+或未实现_method#的类上调用。
def try_方法(方法=nil、*args和block)
如果方法==nil&&block\u给定?
屈服于自己
elsif回应?(方法)
__发送(方法、*args和块)
其他的
无
结束
结束
结束
类零类
#在+nil+上调用+try\u method+总是返回+nil+。
#在浏览可能返回+nil+的关联时,它特别有用。
#
#==示例
#
#nil.try_方法(:name)#=>nil
#
#不使用+try\u方法+
#@person&&@person.response_to(:children)&&@person.children.blank?&&@person.children.response_to(:first)和&@person.children.first.response_to(:name)和&@person.children.first.name
#
#W