dcarneiro
dcarneiro

Reputation: 7150

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?

Upvotes: 7

Views: 12974

Answers (1)

aNoble
aNoble

Reputation: 7072

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

Upvotes: 9

Related Questions