【问题标题】:"TypeError: can't quote" when using custom types and the attributes API使用自定义类型和属性 API 时出现“TypeError: can't quote”
【发布时间】:2018-12-07 15:20:42
【问题描述】:

我在尝试使用 Attributes API 在我的 ActiveRecord 模型中允许自定义类型时遇到了问题。在我尝试建立关系之前,我可以让一切正常。当我这样做时,我收到 TypeError: can't quote 错误。这似乎是由ActiveRecord 在尝试“引用”该属性时引起的。我假设它在引用之前序列化属性。有人看到我缺少的东西吗?

型号:

class MyClass < ActiveRecord::Base
   attribute :my_attr, :my_custom
   has_many :others, primary_key: :my_attr
end

属性类型:

class MyCustomType < ActiveRecord::Type::String

  def deserialize(value)
    MyCustom.new(value)
  end

  def cast(value)
    MyCustom.new(value)
  end

  def serialize(value)
    value.to_s
  end

end

【问题讨论】:

  • 你能添加一个堆栈跟踪吗?
  • 没有太多痕迹。它只是指向调用关系的行。例如“my_class.others”

标签: ruby-on-rails activerecord ruby-on-rails-5


【解决方案1】:

Tom,将自定义类型添加到相应类的属性中。这将告诉 Rails 在处理关系时如何处理两个不同类上的该属性。

class Other < ActiveRecord::Base
   attribute :my_attr, :my_custom
   belongs_to :my_class, primary_key: :my_attr
end

【讨论】:

    猜你喜欢
    • 2013-04-09
    • 1970-01-01
    • 2018-10-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-09-19
    • 1970-01-01
    相关资源
    最近更新 更多