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

Cannot have constant named Collection within Virtus enabled class. #360

Open
stephenprater opened this issue May 25, 2016 · 0 comments
Open

Comments

@stephenprater
Copy link

stephenprater commented May 25, 2016

This bug manifests as a mysterious, difficult to track ArgumentError, and is particularly gnarly to track down because it will only happen with "lazily loaded" constants (like for instance the Rails preloader.)

Say you have a setup like this:

class Thing
   include Virtus.model
end

And in a different file:

class Thing::Collection
end

A call that lazily loads the Thing::Collection will result in the argument error because Virtus const_missing extensions will autoload the Virtus::Attributes::Collection constant.

I don't know enough about Virtus internals to really attempt a fix but this key being present in the TypeLookup cache is probably the salient problem.

:Collection => Virtus::Attribute::Collection < Virtus::Attribute

The problem can be mitigated by either changing the name of your Collection class or explicitly requiring it AFTER your class definition has opened like so.

class Thing
   require 'thing/collection'
   include Virtus.model
end

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

No branches or pull requests

1 participant