E
E
eldar_web2017-09-24 20:04:41
MySQL
eldar_web, 2017-09-24 20:04:41

How to use created_at field in Ruby on Rails in mysql query?

Usually this thing works STR_TO_DATE(string, '%d.%m.%Y'), example:

Post.where("STR_TO_DATE('02.11.2016', '%d.%m.%Y') < STR_TO_DATE('01.01.2017', '%d.%m.%Y')")

But if you use the created_at field, it doesn't work, for example:
Post.where("STR_TO_DATE(created_at, '%d.%m.%Y') < STR_TO_DATE('01.01.2017', '%d.%m.%Y')")

How to solve the problem?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Melkij, 2017-09-26
@eldar_web

From the comments, created_at is a normal timestamp or datetime field. Therefore, not only does it not need to be cast to a string and parsed again into a date (with the wrong format, moreover), but it is also very harmful to do so, because precludes the use of an index.
The date can be formatted in Ymd format on the application, this is already more convenient.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question