-
-
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
http://qiita.com/k0kubun/items/80c5a5494f53bb88dc58
これよくまとまってます。