2013-10-17 51 views
0

我有一個記錄調用的應用程序。每個呼叫可以有多個與之關聯的單位。我的應用程序的一部分有一個報告部分,它基本上只針對不同的標準對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 

因爲我有臺協會已經,我不知道我是否需要爲單位範圍內以某種方式或在結果變量在某種程度上表達了我的結果搜索邏輯。

+0

這麼多的代碼,但我仍然不知道你在問什麼。 'by_unit_name'應該做些什麼?你爲什麼要發佈所有沒有什麼可以做你真正想要的東西?請舉一個你問題出在哪裏的簡單例子,然後在這裏發佈。 – phoet

+0

我正在尋找創建一個結果變量,這將允許我通過單元名稱進行搜索。所以在我的報告視圖中,我可以選擇單位名稱並按單位名稱過濾調用結果。我真的不確定是否需要編寫一個範圍來執行此操作,或者如果我可以在結果變量中表示這一點。我刪除了額外的代碼並評論我需要幫助的地方。 – nulltek

回答

1

基本上,你想要一個使用連接的範圍,所以你可以使用where條件來對付相關的模型?那是對的嗎?

所以在SQL你正在尋找類似

select * from results r 
    inner join call_units c on c.result_id = r.id 
    inner join units u on u.call_unit_id = c.id 
    where u.name = ? 

和範圍將是(從內存中,我還沒有調試這)是這樣的:

scope :by_unit_name, lambda {|unit_name| 
    joins(:units).where('units.unit_name = ?', unit_name) 
} 

units.name不是db中的列。將其更改爲units.unit_name沒有引發異常,似乎是我想要的。下面是我在我的結果變量:

results = results.by_unit_name(search[:unit_name]) if search[:unit_name].present? 

當我嘗試用不同的單位名稱沒有結果顯示搜索。這裏是我用來搜索的代碼:

<%= select_tag "search[unit_name]", options_from_collection_for_select(Unit.order(:unit_name), :unit_name, :unit_name, selected: params[:search].try(:[], :unit_name)), prompt: "Any Unit" %> 
+0

你可以發佈查詢的查詢日誌嗎?您應該能夠看到rails正在嘗試運行的確切SQL(假設您正在開發環境中運行)。 – GSP

+0

SELECT「calls」。* FROM「calls」INNER JOIN「call_units」ON「call_units」。「call_id」=「calls」。「id」INNER JOIN「call_units」「call_units_calls_join」ON「call_units_calls_join」。「call_id」=「調用「。」id「INNER JOIN」units「ON」units「。」id「=」call_units_calls_join「。」unit_id「WHERE(units.unit_name ='1')ORDER BY incident_number ASC LIMIT 10 OFFSET 0 – nulltek

+0

我認爲問題它是選擇unit_name等於unit.id,它是一個整數而不是字符串。所以也許我的select_tag代碼是錯誤的? – nulltek