Augus1986
Augus1986

Reputation: 13

Get all entries for this month only

I want to get a count of all my Offer entries for this month. At the moment I use this to make the count:

@offer_count = Offers.find_all_by_accepted(false).count

This works fine, but as you can see it does count all my entries. So my question is how can I change this to only get the count of the entries of this month.

Also I would like to have the possibility to duplicate this and find all my Offers of an specific month. Like get all offers with accepted false and created in January.

Upvotes: 0

Views: 73

Answers (1)

Andrew Marshall
Andrew Marshall

Reputation: 96934

Use beginning_of_month to compare the date against:

Order.where('created_at >= ?', Time.now.beginning_of_month)

You could of course substitute created_at for updated_at if that's what you want. You can also add on more where conditions if you need (e.g. .where(:accepted => false), as you seem to need in your question).

Upvotes: 1

Related Questions