Closed mike-shock closed 5 years ago
You must have a typo somewhere because the example works, I copied/pasted it from the docs and it worked:
require 'rom-repository'
rom = ROM.container(:sql, 'sqlite::memory') do |config|
config.default.create_table(:users) do
primary_key :id
column :name, String, null: false
column :email, String, null: false
end
config.default.create_table(:tasks) do
primary_key :id
foreign_key :user_id, :users
column :title, String, null: false
end
config.relation(:users) do
schema(infer: true) do
associations do
has_many :tasks
end
end
end
config.relation(:tasks) do
schema(infer: true) do
associations do
belongs_to :user
end
end
end
end
class UserRepo < ROM::Repository[:users]
def create_with_tasks(user)
users.combine(:tasks).command(:create).call(user)
end
end
user_repo = UserRepo.new(rom)
user = user_repo.create_with_tasks(
name: 'Jane',
email: 'jane@doe.org',
tasks: [{ title: 'Task 1' }, { title: 'Task 2' }]
)
user.inspect
# #<ROM::Struct::User id=1 name="Jane" email="jane@doe.org" tasks=[#<ROM::Struct::Task id=1 user_id=1 title="Task 1">, #<ROM::Struct::Task id=2 user_id=1 title="Task 2">]>
I'm trying to run the sample from the page: https://rom-rb.org/5.0/learn/repositories/writing-aggregates/
But the line
users.combine(:tasks).command(:create).call(user)
causes the error: