且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

as_json 中的参数数量错误 [`timeout` 方法被保留!]

更新时间:2023-11-30 23:33:28

我解决了这个问题.来自 Rails 源:

I solved the problem. From the Rails source:

  module Serialization
    def serializable_hash(options = nil)
      options ||= {}

      attribute_names = attributes.keys.sort
      if only = options[:only]
        attribute_names &= Array.wrap(only).map(&:to_s)
      elsif except = options[:except]
        attribute_names -= Array.wrap(except).map(&:to_s)
      end

      hash = {}
      attribute_names.each { |n| hash[n] = read_attribute_for_serialization(n) } # exception here

      # ...
    end

    alias :read_attribute_for_serialization :send

    # ...
  end

  # ...
end

所以真正的错误是调用例如返回的方法之一Unit.first.attributes.keys.sort (["dev_id", "flags", "id", "inote", "ip", "location_id", "model_id", "name", "period", "phone", "port", "snote", "timeout"]) 期望我向它传递一个参数.这个方法是timeout,它是Rails 猴子补丁对象的一些私有方法.所以问题的正确解决方案是将这个属性重命名为别的东西.

So the real error is that one of the methods returned by calling e.g. Unit.first.attributes.keys.sort (["dev_id", "flags", "id", "inote", "ip", "location_id", "model_id", "name", "period", "phone", "port", "snote", "timeout"]) expects me to pass an argument to it. This method is timeout, it is some private method Rails monkey patches Object with. So the correct solution to the problem is to rename this attribute to something else.