Entity class uninitialized in other entity
aladmit opened this issue · comments
I have User
and Message
entities and I want to add users
attribute to Message
.
I have tried to do it like in entities guide: http://hanamirb.org/guides/1.0/models/entities/
# lib/vkbus/entities/user.rb
class User < Hanami::Entity
attributes do
attribute :id, Types::Strict::Int
attribute :username, Types::Strict::String
attribute :first_name, Types::Strict::String
attribute :last_name, Types::Strict::String
end
end
# lib/vkbus/entities/message.rb
class Message < Hanami::Entity
attributes do
attribute :id, Types::Strict::Int
attribute :text, Types::Strict::String
attribute :date, Types::Strict::Time
attribute :users, Types::Collection(User)
end
end
But the code returned an exception:
/app/lib/vkbus/entities/message.rb:7:in `block in <class:Message>': uninitialized constant Message::User (NameError)
After that, I tried to use Hanami::Entity::User
and got the same result.
/app/lib/vkbus/entities/message.rb:7:in `block in <class:Message>': uninitialized constant Hanami::Entity::User (NameError)
At last time, I tried to add messages
attribute to User
for check and I was surprised because it works!
class User < Hanami::Entity
attributes do
...
attribute :messages, Types::Collection(Message) ## works
end
end
class Message < Hanami::Entity
attributes do
...
attribute :users, Types::Collection(User) ## doesn't work
end
How can it be possible? Am I doing something wrong?
P.S. You can find code of my app in this repo: https://github.com/postgred/vkbus
@postgred Code in lib/
directory is eager loaded in alphabetical order.
lib/vkbus/entities/message.rb
is required before of lib/vkbus/entities/user.rb
, that's why it fails to find User
as constant.
To fix this you should add at the top of lib/vkbus/entities/message.rb
require_relative "./user"