-
-
Save tai2/665aefa4bd3427754ceb2fad395339c7 to your computer and use it in GitHub Desktop.
ActiveRecordで未公開の記事を含むカテゴリを抽出する
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Category < ActiveRecord:Base | |
has_many :posts | |
... | |
end | |
# publised :boolean | |
class Post < ActiveRecord:Base | |
... | |
end | |
Category | |
.left_outer_joins(:posts) | |
.group('categories.id') | |
.having('SUM(CASE WHEN NOT posts.published THEN 1 ELSE 0 END) > 0') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
なるほど、全記事が公開されてるカテゴリ一覧だと、各カテゴリごとの件数出して
各カテゴリごとにそれぞれ全部publishedになってるか、みないとできないので
なかなか綺麗にいかないですね。