P
P
Pavel Grudinkin2016-06-28 01:13:36
Ruby on Rails
Pavel Grudinkin, 2016-06-28 01:13:36

How to use logical operators in where queries in rails?

Continuation of the question How to pull out a post with comments on a specific date? :
As in a similar query:

@posts = Post.where(user_id:123).includes(:comments).where(comments: { created_at: Time.zone.today...Time.zone.tomorrow })
, add a similar condition, but on a different field using logical operators?
Those. to get something similar to (doesn't work):
@posts = Post.where(user_id:123).includes(:comments).where(comments: { created_at: Time.zone.today...Time.zone.tomorrow } | comments: { created_at: Time.zone.today...Time.zone.tomorrow })

Answer the question

In order to leave comments, you need to log in

1 answer(s)
K
Karim Kyatlottyavi, 2016-06-28
@Hunt666

Something like that.
The native ability to use the logical OR operator in ActiveRecord exists in Rails 5.
https://github.com/rails/rails/pull/16052
https://github.com/rails/rails/blob/aa3acf85cb/act...
Post.where(id: 1).or(Post.where(id: 2))

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question