Join request in Hanami model

Is it possible to create a connection request in a subclass Hanami::Repository?

I found that this pull request implements this function, but I cannot find it in the current codebase.

+4
source share
1 answer

The Hanami model is rom-based, so you can use the method Relation#joinwith the necessary ratio.

To do this, you need to call the method joinfor one relationship and set another 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 useful links:

+11
source

Source: https://habr.com/ru/post/1673445/


All Articles