mirror of
https://github.com/jbranchaud/til
synced 2026-01-05 08:08:02 +00:00
Add Merge A Scope Into An ActiveRecord Query as a rails til
This commit is contained in:
@@ -10,7 +10,7 @@ smart people at [Hashrocket](http://hashrocket.com/).
|
|||||||
For a steady stream of TILs from a variety of rocketeers, checkout
|
For a steady stream of TILs from a variety of rocketeers, checkout
|
||||||
[til.hashrocket.com](https://til.hashrocket.com/).
|
[til.hashrocket.com](https://til.hashrocket.com/).
|
||||||
|
|
||||||
_818 TILs and counting..._
|
_819 TILs and counting..._
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
@@ -504,6 +504,7 @@ _818 TILs and counting..._
|
|||||||
- [Mark A Migration As Irreversible](rails/mark-a-migration-as-irreversible.md)
|
- [Mark A Migration As Irreversible](rails/mark-a-migration-as-irreversible.md)
|
||||||
- [Make ActionMailer Synchronous In Test](rails/make-action-mailer-synchronous-in-test.md)
|
- [Make ActionMailer Synchronous In Test](rails/make-action-mailer-synchronous-in-test.md)
|
||||||
- [Mark For Destruction](rails/mark-for-destruction.md)
|
- [Mark For Destruction](rails/mark-for-destruction.md)
|
||||||
|
- [Merge A Scope Into An ActiveRecord Query](rails/merge-a-scope-into-an-activerecord-query.md)
|
||||||
- [Migrating Up Down Up](rails/migrating-up-down-up.md)
|
- [Migrating Up Down Up](rails/migrating-up-down-up.md)
|
||||||
- [Params Includes Submission Button Info](rails/params-includes-submission-button-info.md)
|
- [Params Includes Submission Button Info](rails/params-includes-submission-button-info.md)
|
||||||
- [Perform SQL Explain With ActiveRecord](rails/perform-sql-explain-with-activerecord.md)
|
- [Perform SQL Explain With ActiveRecord](rails/perform-sql-explain-with-activerecord.md)
|
||||||
|
|||||||
34
rails/merge-a-scope-into-an-activerecord-query.md
Normal file
34
rails/merge-a-scope-into-an-activerecord-query.md
Normal file
@@ -0,0 +1,34 @@
|
|||||||
|
# Merge A Scope Into An ActiveRecord Query
|
||||||
|
|
||||||
|
Consider an ActiveRecord model with a scope:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
class Book < ApplicationRecord
|
||||||
|
scope :published, -> { where("books.published_at is not null") }
|
||||||
|
end
|
||||||
|
```
|
||||||
|
|
||||||
|
Now let's say we are working in another part of the codebase composing a query
|
||||||
|
that gathers all authors with published books. That might look something like
|
||||||
|
this:
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
published_authors =
|
||||||
|
Authors.joins(:book).where("books.published_at is not null")
|
||||||
|
```
|
||||||
|
|
||||||
|
This will get the job done, but we've now duplicated the same logic in
|
||||||
|
different parts of the app. We can utilize the existing scope on `Book` using
|
||||||
|
ActiveRecord's
|
||||||
|
[`merge`](https://devdocs.io/rails~5.2/activerecord/spawnmethods#method-i-merge)
|
||||||
|
method.
|
||||||
|
|
||||||
|
```ruby
|
||||||
|
published_authors =
|
||||||
|
Authors.joins(:book).merge( Book.published )
|
||||||
|
```
|
||||||
|
|
||||||
|
The `merge` method can be used to incorporate any conditions from other partial
|
||||||
|
queries -- this means both `where` clauses and `joins` clauses.
|
||||||
|
|
||||||
|
[source](http://aokolish.me/blog/2015/05/26/how-to-simplify-active-record-scopes-that-reference-other-tables/)
|
||||||
Reference in New Issue
Block a user