2

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

But Foo in the terminal is returning undefined method after_initialize for Foo:Class

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)

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).

MCB
  • 2,021
  • 1
  • 18
  • 32

1 Answers1

0

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.

Ramon Tayag
  • 15,224
  • 9
  • 43
  • 69
  • 1
    For what it's worth Virtus was also too rigid (or possibly I didn't know how to use Virtus well enough) for what I was trying to do. So, I ended up just writing something custom. It's good to see that this isn't how it's supposed to work, because that is always the fear, do I not understand X or does X just not do this thing? – MCB Feb 13 '17 at 05:32
  • 1
    Yes, also, it complicates the code :) Cool glad to hear you found a solution. – Ramon Tayag Feb 13 '17 at 06:54