P
P
Pavel Grudinkin2016-06-23 03:04:33
Ruby on Rails
Pavel Grudinkin, 2016-06-23 03:04:33

How to pull a post with comments by a specific date?

There is a page with a post, there are comments on how to make posts and all comments to them for a certain date be stored in one variable.
I'm trying to do it this way, but swears at the second where:

@posts = Post.where(user_id:123).joins(:comments).where(comments: {"created_at >= ? AND created_at < ?", "2016-06-23", "2016-06-24"})

By itself, this part works:
Comments.where("created_at >= ? AND created_at < ?", "2016-06-23", "2016-06-24")

Answer the question

In order to leave comments, you need to log in

2 answer(s)
Артем Пьянков, 2016-06-23
@Hunt666

Следующий участок кода - вообще не валидный Ruby, где вы видели хэш без ключей?
То что вы пытаетесь сделать:
Обратите внимание, что тут используется includes вместо joins, т.к. joins не осуществляет предзагрузку записей в память.

D
Dmitry Kovalsky, 2016-06-23
@dmitryKovalskiy

Of course, I'm not an expert on RoR, but I'll try to guess. Do you know in which script this construction is deployed? And what exactly is he complaining about? and what do you think is wrong? Personally, I assume that select is executed with one join, and columns are used in the where clause without specifying the filtering entity. In pure SQL, of course, you can do this, but this is a bit of a lottery due to the fact that both tables can have a field called created_at, and the server, as a rule, is not eager to guess what exactly you want to filter

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question