0

http://rom-rb.org/を試していますが、複数のソース モデルが存在する場合に存在検証を通過させる方法がわかりません。次のスクリプトで新しいイベントとオーガナイザーが保存されると思いますが、それevent_nameが存在しないと表示されます。

私は何が欠けていますか?

require 'bundler/inline'

gemfile do
  source 'https://rubygems.org'
  gem 'rom'
  gem 'rom-sql'
  gem 'rom-rails'
  gem 'activemodel'
  gem 'sqlite3'
  gem 'activesupport'
end

require 'rom'
require 'rom-rails'

`rm -Rf /tmp/romtest.sqlite`
ROM.setup(:sql, 'sqlite:///tmp/romtest.sqlite')

class Events < ROM::Relation[:sql]
end

class Organisers < ROM::Relation[:sql]
end

class CreateEvent < ROM::Commands::Create[:sql]
  relation :events
  register_as :create
  result :one

  associates :organiser, key: [:organiser_id, :id]
end

class CreateOrganiser < ROM::Commands::Create[:sql]
  relation :organisers
  register_as :create
  result :one
end

class CreateEventWithOrganiser < ROM::Model::Form
  commands organisers: :create, events: :create

  input do
    attribute :email
    attribute :event_name
  end

  validations do
    validates :event_name, presence: true
  end

  def commit!
    command = organisers.create.with(
      email: email,
    ) >> events.create.with(
      name: event_name,
    )

    command.transaction do
      command.call
    end
  end
end

ROM.finalize
rom = ROM.env
gateway = rom.gateways.fetch(:default)
migration = gateway.migration do
  change do
    create_table :organisers do
      primary_key :id
      column :email, String, null: false
    end

    create_table :events do
      primary_key :id
      column :name, String, null: false
      column :organiser_id, Integer, null: false
    end
  end
end

migration.apply(gateway.connection, :up)

f = CreateEventWithOrganiser.build(
  email:      'test@example.com',
  event_name: 'Test Event'
)

# Unexpectedly fails
f.save
puts f.errors.full_messages
# => "Event name can't be blank"
4

1 に答える 1