2012-07-13 40 views
0

我有以下的控制器代碼Rails的包括過濾模型JSON對象

respond_with(@employees) do |format| 
    format.json { render :json => @employees.to_json(:include => :shifts) } 
end 

我有什麼做的,如果我想篩選所包含的轉變?例如通過日期。 我必須能夠在控制器中設置過濾器參數。

編輯︰ 我想過使用:方法,但它在json對象中創建另一個變量。它必須被稱爲「輪班」

回答

2
format.json { render :json => @employees.includes(:shifts).where("shifts.date > ?", your_date_here).to_json(:include => :shifts) } 
+0

哇感謝幾乎完美。但它也應該包括沒有任何班次的員工 – Ben 2012-07-13 10:19:20