virtus icon indicating copy to clipboard operation
virtus copied to clipboard

to_h/to_hash should deeply convert attributes to a hash

Open bradrobertson opened this issue 10 years ago • 2 comments

bradrobertson avatar Aug 06 '15 14:08 bradrobertson

Just a heads up here, this blows up if you are using the Array[MyVirtusModel] type.

You can bypass this via:

  class ModelCollection < Array
    def to_h
      map { |model|
        model.to_h
      }
    end
  end

  class Image
    include Virtus.model
    attribute :width,       Integer
    attribute :height,      Integer
    attribute :url,         String
  end

  class Album
    include Virtus.model
    attribute :id,          String
    attribute :title,       String
    attribute :type,        String
    attribute :images,      ModelCollection[Image]
  end

mraaroncruz avatar Oct 08 '15 10:10 mraaroncruz

@bradrobertson @pferdefleisch - thanks for this - I needed to, so I handled the Array case here -

https://github.com/Fooda/virtus/pull/2

in case it helps.

MissingHandle avatar Mar 06 '17 22:03 MissingHandle