mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Get An Empty ActiveRecord Relation as a rails til
This commit is contained in:
33
rails/get-an-empty-activerecord-relation.md
Normal file
33
rails/get-an-empty-activerecord-relation.md
Normal file
@@ -0,0 +1,33 @@
|
||||
# Get An Empty ActiveRecord Relation
|
||||
|
||||
When you query for something (with `#where`) and there are no results matching
|
||||
that query, you get something that looks like an empty array (`[]`), but it's
|
||||
not quite.
|
||||
|
||||
```ruby
|
||||
> result
|
||||
[]
|
||||
> result.class
|
||||
Book::ActiveRecord_Relation
|
||||
```
|
||||
|
||||
It's an empty [`ActiveRecord`
|
||||
relation](https://api.rubyonrails.org/classes/ActiveRecord/Relation.html).
|
||||
|
||||
You can get an instance of an empty `ActiveRecord` relation without
|
||||
constructing a _no result_ query.
|
||||
|
||||
```ruby
|
||||
> Book.none
|
||||
[]
|
||||
> Book.none.class
|
||||
Book::ActiveRecord_Relation
|
||||
```
|
||||
|
||||
I can think of a couple scenarios where this would be useful:
|
||||
|
||||
- as a default value for a method parameter
|
||||
- as a test value for a method that expects to have an `ActiveRecord` relation
|
||||
passed in
|
||||
|
||||
[source](https://stackoverflow.com/questions/4877931/how-to-return-an-empty-activerecord-relation)
|
||||
Reference in New Issue
Block a user