rom-rails icon indicating copy to clipboard operation
rom-rails copied to clipboard

Fails to load a custom struct through Rails autoloading

Open art-solopov opened this issue 6 years ago • 4 comments

How to reproduce

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.

art-solopov avatar Sep 25 '17 21:09 art-solopov

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.

cflipse avatar Oct 12 '17 02:10 cflipse

Did you resolve that ? I cannot fix this

Edit :

It's working with (but not for repository)

module Entities
  require 'entities/user'
end

anthony-bernardo avatar Mar 22 '18 09:03 anthony-bernardo

There is 2 issues :

  1. Hot reloading don't work
  2. Repository don't work

I'm trying to integrate rom on Rails, for now here my result : https://github.com/xero88/poc-rom-rb

anthony-bernardo avatar Mar 22 '18 10:03 anthony-bernardo

  1. Please file a different ticket
  2. I need more information "Repository don't work"
  3. For debugging and advice (ie, until we know what the actual error is) please use https://discourse.rom-rb.org/
  4. 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.

cflipse avatar Mar 22 '18 13:03 cflipse