2012-01-14 24 views
0

我有下面的代碼在我的Rails(3.1)控制器之一:向控制器添加多個字符串查找條件?

@count = Call.where(:destination => current_user.destination_id).count 

@count_day = Call.where('created_at > ?', 1.days.ago).count 
@count_month = Call.where('created_at > ?', 30.days.ago).count 

他們都工作正常,但我試圖以某種方式合併兩個人在一起,因此它顯示創建的呼叫數在過去1天內,但僅適用於目標與當前用戶destination_id值匹配的呼叫。

我已嘗試添加:條件,但沒有快樂:

@count_day = Call.where(:destination => current_user.destination_id, :condition ['created_at > ?', 1.days.ago]).count 

是否有可能以這種方式添加多個條件?如果是這樣,怎麼樣?

回答

0

當創建一個範圍,範圍可以鏈接,這樣你就可以做

Call.where(:destination =>current_user.id).where("created_at > ?", 1.day.ago).count 
+0

由於某些原因輸出'#'而不是呼叫計數。 – dannymcc 2012-01-14 15:31:54

+0

如果你想要計數,最後加上'.count' - 我只是說明'哪裏可以鏈接' – 2012-01-14 16:28:09

1

試試這個:

@count_day = Call.where("destination = :destination AND created_at > :date", { :destination => current_user.destination_id, :date => 1.days.ago}).count 
+0

如果我用你的代碼'@count_day = Call.where(:條件= > ['destination =(?)AND created_at>(?)',current_user.destination_id,1.days.ago])。count'然後我得到以下錯誤:SQLite3 :: SQLException:no such column:calls.conditions: SELECT COUNT(*)FROM「calls」WHERE「calls」。「conditions」IN('destination =(?)AND created_at>(?)','01772232427','2012-01-13 12:57:40.453694') ' – dannymcc 2012-01-14 12:59:13

+0

我已經更新了我的答案,現在應該可以正常工作。 – 2012-01-14 14:11:14