How to setup Ruby Object Mapper (ROM) for standalone project

Igor Kuznetsov
Mar 11 · 5 min read
Company & Post relations

Project setup

$ mkdir rom-sample-app && cd rom-sample-app
$ touch Gemfile
$ bundle install
In boot.rb we setup all dependencies from Gemfile.
$ touch console
$ chmod +x console

Working with MySQL from ROM

Connecting ROM & MySQL

Working with ROM migrations

ROM migrations
$ bundle exec rake db:create_migration[create_companies]
$ bundle exec rake db:create_migration[create_posts]
$ bundle exec rake db:migrate
<= db:migrate executed
$ ./console
pry(main)> MAIN_CONTAINER.
=> [[:id,
[:updated_at, {:primary_key=>false, :generated=>false, :allow_null=>true, :default=>nil, :db_type=>"datetime", :type=>:datetime, :ruby_default=>nil}]]

ROM Relations

ROM Relations for Company and Post
configuration.register_relation(Companies, Posts)
pry(main)> MAIN_CONTAINER.relations[:companies].count
=> 0
pry(main)> MAIN_CONTAINER.relations[:posts].count
=> 0

ROM Commands

use timestamps
timestamp :created_at, :updated_at
configuration.register_command(CreateCompany, DeleteCompany)
configuration.register_command(CreatePost, UpdatePost, DeletePost)
[1] pry(main)> companies = MAIN_CONTAINER.relations[:companies]
[2] pry(main)> companies.command(:create).call(
name: 'My 1st Company', domain: '')
=> {:id=>1,
:name=>"My 1st Company",
:created_at=>2019-03-09 14:24:23 +0000,
:updated_at=>2019-03-09 14:24:23 +0000}

Testing with ROM


Welcome to a place where words matter. On Medium, smart voices and original ideas take center stage - with no ads in sight. Watch
Follow all the topics you care about, and we’ll deliver the best stories for you to your homepage and inbox. Explore
Get unlimited access to the best stories on Medium — and support writers while you’re at it. Just $5/month. Upgrade