Reputation: 1044
structure of my application:
.
├── config
│ ├── boot.rb
│ └── environment.rb
├── Gemfile
├── Gemfile.lock
├── lib
│ ├── entities
│ │ └── account.rb
│ └── repositories
│ └── account_repository.rb
└── README.md
Gemfile:
source 'https://rubygems.org'
gem 'pg', '~> 1.1'
gem 'dotenv'
gem 'byebug'
gem 'hanami-model'
config/environment.rb:
require 'bundler/setup'
require 'hanami/model'
require 'dotenv/load'
class App
class << self
def boot
Mutex.new.synchronize do
Hanami::Model.configure do
adapter :sql, ENV['DATABASE_URL']
end.load!
end
end
end
end
config/boot.rb:
require_relative './environment'
App.boot
lib/entities/account.rb:
require 'hanami/model'
require_relative '../repositories/account_repository'
class Account < Hanami::Entity
end
lib/repositories/account_repository.rb:
require 'hanami/model'
require_relative '../entities/account'
class AccountRepository < Hanami::Repository
self.relation = :accounts
end
in the console, I run the following code, and I get an error:
irb -I .
irb(main):001:0> require 'config/boot'
=> true
irb(main):002:0> require 'lib/repositories/account_repository'
=> true
irb(main):003:0> rep = AccountRepository.new
Traceback (most recent call last):
6: from /home/mvalitov/.asdf/installs/ruby/2.5.1/bin/irb:11:in `<main>'
5: from (irb):3
4: from (irb):3:in `new'
3: from /home/mvalitov/.asdf/installs/ruby/2.5.1/lib/ruby/gems/2.5.0/gems/hanami-model-1.3.2/lib/hanami/repository.rb:420:in `initialize'
2: from /home/mvalitov/.asdf/installs/ruby/2.5.1/lib/ruby/gems/2.5.0/gems/rom-repository-1.4.0/lib/rom/repository/root.rb:62:in `initialize'
1: from /home/mvalitov/.asdf/installs/ruby/2.5.1/lib/ruby/gems/2.5.0/gems/rom-3.3.3/lib/rom/registry.rb:30:in `fetch'
ArgumentError (key cannot be nil)
what am I doing wrong? if you put all the entity code and repositories in one file, the code runs without errors.
Upvotes: 3
Views: 222
Reputation: 990
The issue is in order of load.
Here is a note about strict order in the documentation:
When using a sql adapter, you must require hanami/model/sql before Hanami::Model.load! is called so the relations are loaded correctly.
Source: https://github.com/hanami/model#mapping
Hence, for your case, need to run App.boot
after all declarations.
In details:
If you put all your code into one file you will see differences:
# run.rb
require 'bundler/setup'
require 'hanami/model'
require 'dotenv/load'
class App
class << self
def boot
Mutex.new.synchronize do
Hanami::Model.configure do
adapter :sql, 'postgresql://postgres:12345@localhost:5432/mame-challenge_development'
path '/home/mifrill/Documents/source/hamani-bug'
end.load!
end
end
end
end
App.boot
class AccountRepository < Hanami::Repository
self.relation = :accounts
end
class Account < Hanami::Entity
end
AccountRepository.new
ruby run.rb
gems/rom-3.3.3/lib/rom/registry.rb:30:in `fetch': key cannot be nil (ArgumentError)
Move App.boot
after Repository and Entity definitions, like this:
require 'bundler/setup'
require 'hanami/model'
require 'dotenv/load'
class App
class << self
def boot
Mutex.new.synchronize do
Hanami::Model.configure do
adapter :sql, 'postgresql://postgres:12345@localhost:5432/mame-challenge_development'
path '/home/mifrill/Documents/source/hamani-bug'
end.load!
end
end
end
end
class AccountRepository < Hanami::Repository
self.relation = :accounts
end
class Account < Hanami::Entity
end
App.boot
AccountRepository.new
ruby run.rb
{:accounts=>#<ROM::Relation[Accounts] name=accounts dataset=#<Sequel::Postgres::Dataset: "SELECT * FROM \"accounts\"">>
So, try to require Repository file before load!
to solve:
config/boot.rb
:
require_relative './environment'
require_relative '../lib/repositories/account_repository'
App.boot
irb -I .
require 'config/boot'
rep = AccountRepository.new
...
{:accounts=>#<ROM::Relation[Accounts] name=accounts dataset=#<Sequel::Postgres::Dataset: "SELECT * FROM \"accounts\"">>}
Upvotes: 1
Reputation: 1895
You are missing the "root" configuration/options (I don't know much as I don't use Hanami).
Looking at the stacktrace:
fetch
has an alias :[]
(all links to latest because I'm lazy :) )
Upvotes: 0