问题
I have the following code in one of my Rails (3.1) controllers:
@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
They are all working as expected, but I am trying to somehow merge two of them together so it shows the count of calls created in the last 1 day, but only for calls with a destination that matches the current users destination_id value.
I have tried to add :condition but with no joy:
@count_day = Call.where(:destination => current_user.destination_id, :condition ['created_at > ?', 1.days.ago]).count
Is it possible to add more than one condition in this way? If so, how?
回答1:
Where creates a scope and scopes can be chained, so you can do
Call.where(:destination =>current_user.id).where("created_at > ?", 1.day.ago).count
回答2:
Try this:
@count_day = Call.where("destination = :destination AND created_at > :date", { :destination => current_user.destination_id, :date => 1.days.ago}).count
来源:https://stackoverflow.com/questions/8862065/adding-more-than-one-string-to-a-controller-find-condition