A
A
Alexander Petrov2016-01-20 11:41:22
Ruby on Rails
Alexander Petrov, 2016-01-20 11:41:22

How to display articles for a specific month in ruby ​​on rails?

Actually. There are articles, you need to make a "where" filter so that articles for a certain month of all years are displayed. I thought to make the minimum and maximum dates, for example, for February it is 02/01/2015 and 02/31/2015 and display everything that is included in this section. But in a year, you will have to add another year to the filter, and this is no longer interesting. How to make this filter in general, tell me, please?

Answer the question

In order to leave comments, you need to log in

4 answer(s)
V
Veniamin Chebotarev, 2016-01-20
@bj7smth

Show how you already wrote the filter code. In general, the year can be passed as a parameter.

B
bukovki, 2016-01-20
@bukovki

Something like this:

@items = Item.where(
updated_at: Time.parse(params[:date])
.beginning_of_month..Time.parse(params[:date]).end_of_month

C
Cobalt the Terrible, 2016-01-20
@CobaltTheTerrible

Model.where("MONTH(created_at) = ?", month)
See www.tutorialspoint.com/sql/sql-date-functions.htm#...

C
caution, 2016-01-21
@caution

We take the oldest and newest record, we take the newest and oldest year, thus we solve the problem with years. Either Time.now.year

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question