seb
seb

Reputation: 129

Date range in Rails

I´m looking to find "books" between specific dates.

I have a variable publishdate and want to get all the books which have a publish date between to dates, but I´m not sure which operator to use.

I tought of something like this but it´s not working

@books = Book.where(:e_release_date => 20141217 && :e_release_date <= 20141017)

Upvotes: 0

Views: 261

Answers (2)

Rajarshi Das
Rajarshi Das

Reputation: 12320

You can try this for date1 and date2

data = Book.where(e_release_date: Date.parse(params[:date1])..Date.parse(params[:date2]))

It will give you data for date 2014-10-17 and 2014-12-17

SELECT * FROM books WHERE (books.e_release_date BETWEEN '2014-10-17 00:00:00' AND '2014-12-17 00:00:00')

Upvotes: 2

shivam
shivam

Reputation: 16506

I'm assuming you are using ActiveRecord. In ActiveRecord you can query between like this:

@books = Book.where(:e_release_date => 20141217..20141017)

Upvotes: 4

Related Questions