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

Recursively call attributes #290

Open
jaimerson opened this issue Aug 22, 2014 · 8 comments
Open

Recursively call attributes #290

jaimerson opened this issue Aug 22, 2014 · 8 comments
Labels
Milestone

Comments

@jaimerson
Copy link

jaimerson commented Aug 22, 2014

Suppose I have a class Foo, which has an array of Bar objects, as follows:

class Foo
  include Virtus.model
  attribute :bars, Array[Bar]
end

class Bar
  include Virtus.model
  attribute :baz
  attributes :bamboo
end

Is there a way to retrieve a attributes hash for each Bar ({ bars: [ {baz:1, bamboo: 2}] }) when I call Foo#attributes? Calling attributes gives me this:

Foo.new(bars: [{baz: 1, bamboo: 2}]).attributes
# => { bars: [#<Bar:0xfd54ad3, @baz=1, @bamboo=2>] }

@AlfonsoUceda
Copy link

I look for something like that.

@solnic solnic added this to the 2.0.0 milestone Sep 11, 2014
@solnic
Copy link
Owner

solnic commented Sep 11, 2014

This will be addressed in virtus 2.0 refactoring. #attributes (and effectively #to_h and #to_hash) will be removed from virtus' core and moved to some external gem

@AlfonsoUceda
Copy link

thanks @solnic ;)

@bradrobertson
Copy link

This would be useful to me as well. IMO to_h and to_hash should not alias attributes but instead return a pure hash. Would a PR in the interim modifying to_h and to_hash to return pure hashes be useful?

Unless 2.0 is right around the corner, but it doesn't look like that's the case.

@solnic
Copy link
Owner

solnic commented Jul 15, 2015

@bradrobertson we can add that with a limitation that when there are cross-references it will blow up with a stack too deep. You could use a recursion guard too but that will slow it down. I think it's fine to clearly document it and call it a day.

@derekclee
Copy link

It seems like to_json works as expected by converting all nested models. So a workaround might be JSON.parse(model.to_json).

@ilnurnasyrov2
Copy link

It seems like to_json works as expected by converting all nested models. So a workaround might be JSON.parse(model.to_json).

looks like this workaround doesn't work anymore

require 'virtus'

class User
  include Virtus.model
end

User.new.to_json #=> NoMethodError: undefined method `to_json' for #<User:0x007fed648427c8>

@filipesperandio
Copy link

Is there a recommended way to accomplish this?

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

No branches or pull requests

7 participants