4

是否可以在子类中创建连接查询Hanami::Repository

我发现这个拉取请求实现了这个功能,但我在当前的代码库中找不到它。

4

1 回答 1

12

基于 rom 的 Hanami 模型,这就是为什么您可以使用Relation#join具有必要关系的方法。

为此,您需要join为一个关系调用方法并将其他关系设置为属性:

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

就这样。

一些有用的链接:

  1. rom-sql 测试left_join
  2. 一个真实的例子
于 2017-04-01T11:30:07.940 回答