Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count with multiple conditions in Rails

I'm trying to count all the records between 2 dates that are finished. This means that the created_at field is between start_date and end_date and the finished_at field is not null.

I can use the following expression to get the records that didn't finish:

Record.count(:all, :conditions => {:created_at => start_date..end_date, :finished_at => nil })

Is there a similar way to count the records where finished at is not nil?

like image 656
dcarneiro Avatar asked Mar 27 '12 10:03

dcarneiro


1 Answers

This should work just fine unless I'm missing something.

Record.where(:created_at => start_date..end_date).where('finished_at IS NOT NULL').count
like image 118
aNoble Avatar answered Oct 20 '22 23:10

aNoble