首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >将Storext (或仅仅是Virtus)与嵌套数组或散列对象一起使用

将Storext (或仅仅是Virtus)与嵌套数组或散列对象一起使用
EN

Stack Overflow用户
提问于 2016-04-28 23:05:51
回答 1查看 916关注 0票数 2

我有一个postgres DB用一个带有jsonb列的类支持我的Rails应用程序

代码语言:javascript
复制
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

是否有一种方法可以像使用Storext那样嵌套Virtus模型?如果是这样的话,是否有一种惯用的方法在嵌套类上添加验证呢?(放弃Storext,纯Virtus解决方案也能回答这个问题)

也许这是一个A/B问题,因为我只是在FooCollection中包含了Virtus,并且它也从data哈希中消失了(我认为这很奇怪,因为Storext是基于Virtus的,可以接受Virtus方法)。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2017-02-13 01:42:07

Storext仅用于类型转换简单值。我本人也没有尝试过在Virtus中存储复杂的对象,我还没有决定这是否应该是Storext的一部分。虽然它在后台使用Virtus,但我认为它不应该实现Virtus所能做的一切。

派对有点晚了,但我希望这能帮上忙。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/36926927

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档