Ho cercato i documenti su come implementare le relazioni tra entità (ad esempio, uno-a-molti, molti-a-molti, ecc.), Ma non ho trovato nessun esempioImplementazione di relazioni molti-a-molti (e di altri) con Hanami
Così ho provato una ipotesi ragionevole. Ecco il mio tentativo di attuare una Person
che può essere etichettato con Tag
s:
require 'moocho_query'
require 'hanami/model'
require 'hanami/model/adapters/file_system_adapter'
class Person
include Hanami::Entity
attributes :name, :age, :tags
end
class Tag
include Hanami::Entity
attributes :name
end
class PersonRepository
include Hanami::Repository
end
class TagRepository
include Hanami::Repository
end
Hanami::Model.configure do
adapter type: :file_system, uri: './file_db'
mapping do
collection :people do
entity Person
repository PersonRepository
attribute :id, Integer
attribute :name, String
attribute :age, Integer
attribute :tags, type: Array[Tag]
end
collection :tags do
entity Tag
repository TagRepository
attribute :id, Integer
attribute :name, String
end
end
end.load!
me = Person.new(name: 'Jonah', age: 99)
t1 = Tag.new(name: 'programmer')
t2 = Tag.new(name: 'nice')
me.tags = [t1, t2]
PersonRepository.create(me)
Questo fallisce sulla chiamata load!
, con il seguente errore:
/Users/x/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/hanami-utils-0.7.0/lib/hanami/utils/class.rb:90:in `load_from_pattern!': uninitialized constant (Hanami::Model::Mapping::Coercers::{:type=>[Tag]}|
{:type=>[Tag]}) (NameError)
from /Users/jg/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/hanami-model-0.6.0/lib/hanami/model/mapping/attribute.rb:80:in `coercer'
from /Users/jg/.rbenv/versions/2.1.2/lib/ruby/gems/2.1.0/gems/hanami-model-0.6.0/lib/hanami/model/mapping/attribute.rb:53:in `load_coercer'
Qual è il modo corretto per implementare le relazioni in Hanami?
Grazie per le informazioni. Conoscete altre gemme di rubini che implementano il modello di repository con adattatori sostituibili per file/memoria/postgres come Hanami, ma supportano anche associazioni? – Jonah