Umesh Malhotra
Umesh Malhotra

Reputation: 1047

How to filter rails model by month

I want to get only those records of my model which belong to a particular month. I am using:

Order.where(created_at.strftime("%B"): "April")

where created_at is DateTime. created_at.strftime("%B") gives me month, but it does not work. Any alternative?

Upvotes: 0

Views: 971

Answers (1)

Will Richardson
Will Richardson

Reputation: 7960

You'll probably have to do this in plan SQL (not the activerecord DSL):

Order.where("strftime('%m', created_at) = ?", 'April')

This uses the SQLite function to extract the month name

(I haven't done Rails in a while, let me know if this doesn't work)

Upvotes: 1

Related Questions