Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Where with OR condition for the same column in Rails

I'm trying to retrieve all records that have expired_at as nil and expired_at greater than the current time.

I've tried this:

PriceRule.where("expired_at > ? OR expired_at = ?", DateTime.now, nil)

But that is only returning me the records expired_at greater than DateTime.now. Why is the nil being neglected?

like image 616
B-M Avatar asked Jul 31 '20 19:07

B-M


People also ask

What is ApplicationRecord in rails?

Now ApplicationRecord will be a single point of entry for all the customizations and extensions needed for an application, instead of monkey patching ActiveRecord::Base. Say I want to add some extra functionality to Active Record. This is what I would do in Rails 4.2.

What is ActiveRecord in rails?

What is ActiveRecord? ActiveRecord is an ORM. It's a layer of Ruby code that runs between your database and your logic code. When you need to make changes to the database, you'll write Ruby code, and then run "migrations" which makes the actual changes to the database.

What's the purpose of ActiveRecord?

Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database. It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping system.

What does where return in rails?

where returns an ActiveRecord::Relation (not an array, even though it behaves much like one), which is a collection of model objects. If nothing matches the conditions, it simply returns an empty relation. find (and its related dynamic find_by_columnname methods) returns a single model object.

How to join two columns where conditions are chaining with and?

By default, where conditions are chaining with AND operator. To join via OR operator, you will have to use orWhere which we will talk next. ... ->where('column','operator','value') ->where('column','operator','value') ...

How to create a conditional column from more than 2 choices?

Creating a conditional column from more than 2 choices Step 1: Combine price lists together and set fruit column as index The first step is to combine all price lists into one... Step 2: Incorporate Numpy select () with Pandas DataFrame

How to form multiple where clause with and and or conditions?

Laravel Eloquent where multiple AND and OR clause If you are looking to form a query that contains multiple where clause with AND and OR conditions, Here is how you can do it. For example if you have to translate following query in Eloquent SELECT * FROM Users WHERE Country='Germany' AND (City='Berlin' OR City='München');

When to use (equals) condition in SQL?

Remember: These conditions can be used in any kind of database query - Select, Insert , Update or Delete conditions as well as in sub-queries. If you don't provide comparison operator in condition, by default = (equals) operator is used.


Video Answer


2 Answers

You can also take advantage of Rails or here, which will take care of having IS NULL instead of = NULL. I would also recommend using Time.current instead, which has better support for time zones.

PriceRule
  .where(expired_at: Time.current..)
  .or(PriceRule.where(expired_at: nil))
like image 121
Eyeslandic Avatar answered Sep 19 '22 03:09

Eyeslandic


Your current query checks that expired_at is equal to null (via = NULL) which will not evaluate to true. Instead, you want to query that the column is a null value by using IS NULL.

Therefore, the query can be tweaked to the following:

PriceRule.where("expired_at > ? OR expired_at IS NULL", DateTime.now)

Or, if you wish to keep your original argument structure, you can pass nil as before:

PriceRule.where("expired_at > ? OR expired_at IS ?", DateTime.now, nil)
like image 33
Zoran Avatar answered Sep 18 '22 03:09

Zoran