我有一個記錄調用的應用程序。每個呼叫可以有多個與之關聯的單位。我的應用程序的一部分有一個報告部分,它基本上只針對不同的標準對Call模型進行查詢。我已經想出瞭如何編寫一些我想要的範圍,並將它們鏈接到我的報告搜索功能的結果。但我無法弄清楚如何用「單元」進行搜索。下面是從我的代碼相關摘錄:有關聯和ActiveRecord的範圍
Call.rb
has_many :call_units
has_many :units, through: :call_units
#Report search logic
def self.report(search)
search ||= { type: "all" }
# Determine which scope to search by
results = case search[:type]
when "open"
open_status
when "canceled"
cancel
when "closed"
closed
when "waitreturn"
waitreturn
when "wheelchair"
wheelchair
else
scoped
end
#Search results by unit name, this is what I need help with. Scope or express otherwise?
results = results. ??????
results = results.by_service_level(search[:service_level]) if search[:service_level].present?
results = results.from_facility(search[:transferred_from]) if search[:transferred_from].present?
results = results.to_facility(search[:transferred_to]) if search[:transferred_to].present?
# If searching with BOTH a start and end date
if search[:start_date].present? && search[:end_date].present?
results = results.search_between(Date.parse(search[:start_date]), Date.parse(search[:end_date]))
# If search with any other date parameters (including none)
else
results = results.search_by_start_date(Date.parse(search[:start_date])) if search[:start_date].present?
results = results.search_by_end_date(Date.parse(search[:end_date])) if search[:end_date].present?
end
results
end
因爲我有臺協會已經,我不知道我是否需要爲單位範圍內以某種方式或在結果變量在某種程度上表達了我的結果搜索邏輯。
這麼多的代碼,但我仍然不知道你在問什麼。 'by_unit_name'應該做些什麼?你爲什麼要發佈所有沒有什麼可以做你真正想要的東西?請舉一個你問題出在哪裏的簡單例子,然後在這裏發佈。 – phoet
我正在尋找創建一個結果變量,這將允許我通過單元名稱進行搜索。所以在我的報告視圖中,我可以選擇單位名稱並按單位名稱過濾調用結果。我真的不確定是否需要編寫一個範圍來執行此操作,或者如果我可以在結果變量中表示這一點。我刪除了額外的代碼並評論我需要幫助的地方。 – nulltek