rom-rails
rom-rails copied to clipboard
Fails to load a custom struct through Rails autoloading
How to reproduce
- Set up a Rails project
- Make a User "model":
- Insert some test user names and emails
- Run this code in
rails c
:
user_repo = UserRepository.new(ROM.env)
u = user_repo.users.first
puts u.login
Expected
Some string printed
Actual
ROM::Struct::MissingAttribute: undefined method `login' for #<Entities::User:0x005644dfbd85b0> (not loaded attribute?)
Additional info
This seems to be an autoloading issue since setting config.eager_load
to true
produces the expected result.
The full code of the Rails project can be found here.
Unfortunately, the way that the loading structure works in ROM-repository requires the entities module to already exist; otherwise, it will simply create a new one -- which will kill the Rails const_missing
logic.
The best recommendation I have for you is to create a file app/models/entities.rb
and have that file either require all of your entities, or register them with autoload
:
module Entities
autoload :user 'entities/user'
end
Then require 'entities'
at the start of your repository files. It's off the Rails way, I know, and I'll need to make sure that the docs do a better job of mentioning this -- though this is a relatively new feature for rom-repository, and I'm not sure all the docs have been written for it yet.
Did you resolve that ? I cannot fix this
Edit :
It's working with (but not for repository)
module Entities
require 'entities/user'
end
There is 2 issues :
- Hot reloading don't work
- Repository don't work
I'm trying to integrate rom on Rails, for now here my result : https://github.com/xero88/poc-rom-rb
- Please file a different ticket
- I need more information "Repository don't work"
- For debugging and advice (ie, until we know what the actual error is) please use https://discourse.rom-rb.org/
- Looking at your gemfile, I see that you're using Dry::AutoInject, and Spring. Try it without spring, and possibly try it without auto inject, if that's used in any of the repo or related. Both of those screw with the load process, and it may not be in compatible ways. If still failing, open a new ticket.