ActiveRecord Scoped Association
以下のような記述の場合、WHEREに条件がいく
code:ruby
class Author
has_many :articles
end
class Article
belongs_to :author
end
Author.eager_load(:published_articles).where(article: { published: true })
code:sql
SELECT *
FROM
"authors"
LEFT OUTER JOIN "articles"
ON "articles"."author_id" = "authors"."id"
WHERE
"articles"."published" = true
SQLでWHEREではなくJOINに条件を書きたいとき、associationの宣言時、第2引数にscopeを定義する eager_load, joinsなどによるJOINでscopeに定義した条件を適用してくれる
code:ruby
class Author
has_many :published_articles, -> { where(published: true) }
end
Author.eager_load(:published_articles)
code:sql
SELECT *
FROM
"authors"
LEFT OUTER JOIN "articles"
ON "articles"."author_id" = "authors"."id"
AND "articles"."published" = true
table同士の関係が1:Nであってもhas_oneとScoped Associationによって1:1であるかのように扱うこともできる
これはscoped associationを使わずとも実現できるが、使うと便利
code:ruby
class Author
has_one :latest_article, -> { where(latest: true) }
end
authors = Author.eager_load(:latest_article)
author.latest_article
# => arrayではなくArticleのインスタンスが得られる