4

Is it possible to create join query in subclass of Hanami::Repository?

I found that this pull request implements this feature but I can't find it in current codebase.

WojciechKo
  • 1,511
  • 18
  • 35
  • Hi, `Hanami-Model` is based on `rom-rb`. Take a look at its documentation to find out to make a join query http://rom-rb.org/learn/sql/joins/ – NickGnd Apr 01 '17 at 09:30

1 Answers1

12

Hanami model based on rom, that's why you can use Relation#join method with a needful relation.

For this you need to call join method for one relation and set other relation as an attribute:

class PostRepository < Hanami::Repository
  associations do
    has_many :comments
  end

  # ...

  def join_example(date_range)
    posts    # => posts relation
    comments # => comments relation


    posts
      .join(comments) # set relation object here
      .where(comments[:created_at].qualified => date_range)
      .as(Post).to_a
  end
end

And that's all.

Some helpful links:

  1. rom-sql tests for left_join
  2. A real example
Anton
  • 287
  • 2
  • 9
  • He can also do this as `aggregate(:comments).where(comments[:created_at].qualified => date_range).as(Post).to_a` since he has the one-to-many association defined. – Mereghost May 09 '17 at 21:51