Version 3.3

SQL » Joins

To load associated relations you can simply use join, left_join, or right_join.

Using joins with relations

class Users < ROM::Relation[:sql]
  schema(infer: true) do
    associations do
      has_many :tasks
      has_many :posts
    end
  end

  def with_tasks
    join(tasks)
  end

  def with_posts
    left_join(posts)
  end
end

Using joins with explicit name and options

If you want to have more control, you can pass table name and additional options yourself:

class Users < ROM::Relation[:sql]
  schema(infer: true) do
    associations do
      has_many :tasks
      has_many :posts
    end
  end

  def with_tasks
    join(:tasks, id: :user_id, priority: 1)
  end

  def with_posts
    left_join(:posts, id: :user_id)
  end
end

Using joins with additional options

The second option hash can be used too, if you want to provide more options:

class Users < ROM::Relation[:sql]
  schema(infer: true) do
    associations do
      has_many :tasks
      has_many :posts
    end
  end

  def with_tasks
    join(:tasks, { id: :user_id }, table_alias: :user_tasks)
  end

  def with_posts
    left_join(posts, { id: :user_id }, table_alias: :user_posts)
  end
end

Learn more

Check out API docs: