问题描述
我有一个Postgres DB用一个带有jsonb列的类来支持我的Rails应用
I have a postgres DB backing my Rails app with a class with a jsonb column
class Product < AR::B
include Storext.model(data: {})
store_attributes :data do
thing_one String
thing_two Boolean # Not actually showing up in the `data` hash
foos FooCollection[Foo]
end
end
class FooCollection < Array
def <<(obj)
if Hash
super(Coupon.new(obj)
else
# Other coersions
end
end
end
class Foo
include Storext.model
attribute :id, Integer
attribute :price, Float
attribute :regular_price, Float
end
但是终端中的Foo正在返回undefined method after_initialize for Foo:Class
But Foo in the terminal is returning undefined method after_initialize for Foo:Class
是否有一种方法可以嵌套Storext
模型,就像使用Virtus
一样?如果是这样,是否有一种惯用的方法可以在嵌套类上添加验证? (放弃Storext,而单纯的Virtus解决方案也将回答这个问题)
Is there a way to nest Storext
models the same way it is with Virtus
? And if so is there an idiomatic way to add validations on the nested classes? (Abandoning Storext, and a pure Virtus solution would also answer the question)
也许这是一个A/B问题,因为我只是将Virtus包含在FooCollection中,并且它也从data
哈希中消失了(我认为这很奇怪,因为Storext
基于Virtus
并且可以接受Virtus
方法).
Maybe this is an A/B problem because I just included Virtus in FooCollection and it also disappeared from the data
hash (which I consider weird since Storext
is based on Virtus
and can accept Virtus
methods).
推荐答案
Storext的创建仅用于类型转换简单值.我本人并没有在Virtus中存储复杂的对象,我仍然不确定是否应该将其存储在Storext中.尽管它在后台使用Virtus,但我认为它不应实现Virtus可以做的所有事情.
Storext was created to type-cast simple values only. I haven't played around storing complex objects in Virtus myself, and I'm still undecided whether or not that should be part of Storext. While it uses Virtus in the background, I don't think it should implement everything Virtus can do.
聚会晚了一点,但我希望这会有所帮助.
A little late to the party, but I hope this helps.
这篇关于将Storext(或仅Virtus)与嵌套数组或哈希对象一起使用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!