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

Class not getting initialized #368

Open
jayceef opened this issue Sep 20, 2016 · 1 comment
Open

Class not getting initialized #368

jayceef opened this issue Sep 20, 2016 · 1 comment

Comments

@jayceef
Copy link

jayceef commented Sep 20, 2016

I have a normal class in my Rails 5 app:

class Routine < ApplicationRecord
belongs_to :user
end

irb: a= Routine.new(:elevation => 2343)
=> #<Routine:0x007ff2a19c2ea8
elevation: 2343

But when I add the Virtus gem the class doesn't get initialized:

class Routine < ApplicationRecord
include Virtus.model
belongs_to :user
attribute :elevation, Integer
end

irb: ad = Routine.new(:elevation => 133)
=>< Routine:0x007ff29e860e78 not initialized>

@elskwid
Copy link
Collaborator

elskwid commented Sep 20, 2016

@jayceef I haven't used Rails 5 but it looks like the not initialized message comes from an inspect method that is checking if @attributes exist on the model. (see here). My guess is you need to implement an initialize method of your own and call super.

Having said that, I don't think it's a good idea to mix Virtus and ApplicationRecord/ActiveRecord. I've had luck using it with ActiveModel but it's pretty tricky.

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

2 participants