Skip to content

Latest commit

 

History

History
39 lines (29 loc) · 1.08 KB

merging_scopes.md

File metadata and controls

39 lines (29 loc) · 1.08 KB

Using merge to cleanup scopes that use associations

Say we have two models that are associated and one of them has a scope:

class Author < ActiveRecord::Base
    has_many :books
end

class Book < ActiveRecord::Base
  belongs_to :author
  scope :available, ->{ where(available: true) }
end

Let's say we want to join the tables to find all Authors who have books that are available. Without ActiveRecord#merge, we have to make this query:

Author.joins(:books).where("books.available = ?", true)

The generated SQL request is :

SELECT "authors".* FROM "authors" INNER JOIN "books" ON "books"."author_id" = "authors"."id" WHERE "books"."available" = 't'

But with ActiveRecord#merge, this becomes a whole lot cleaner and we don't duplicate the available scope :

Author.joins(:books).merge(Book.available)

The generated SQL request is the same :

SELECT "authors".* FROM "authors" INNER JOIN "books" ON "books"."author_id" = "authors"."id" WHERE "books"."available" = 't'

Source