簡體   English   中英

在Hanami模型中加入查詢

[英]Join query in Hanami-model

是否可以在Hanami::Repository子類中創建連接查詢?

我發現這個 pull請求實現了這個功能,但我在當前的代碼庫中找不到它。

基於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. left_join rom-sql測試
  2. 一個真實的例子

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM