Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

to_h/to_hash should deeply convert attributes to a hash #332

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

bradrobertson
Copy link

No description provided.

@mraaroncruz
Copy link

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

@MissingHandle
Copy link

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

Fooda#2

in case it helps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants