【发布时间】: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