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?
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? 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.
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.
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.
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') ...
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
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');
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.
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))
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)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With