{"provider_url":"https://hatena.blog","published":"2020-12-30 20:34:25","author_name":"ohbarye","categories":["Ruby","Ruby on Rails","ActiveRecord","ohbarye Advent Calendar 2020"],"url":"https://ohbarye.hatenablog.jp/entry/2020/12/30/active-record-association-extensions","title":"ActiveRecord Association extensions\u3067\u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3059\u308b","provider_name":"Hatena Blog","image_url":"https://avatars3.githubusercontent.com/u/1811616","height":"190","blog_url":"https://ohbarye.hatenablog.jp/","html":"<iframe src=\"https://hatenablog-parts.com/embed?url=https%3A%2F%2Fohbarye.hatenablog.jp%2Fentry%2F2020%2F12%2F30%2Factive-record-association-extensions\" title=\"ActiveRecord Association extensions\u3067\u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3059\u308b - valid,invalid\" class=\"embed-card embed-blogcard\" scrolling=\"no\" frameborder=\"0\" style=\"display: block; width: 100%; height: 190px; max-width: 500px; margin: 10px 0px;\"></iframe>","blog_title":"valid,invalid","type":"rich","author_url":"https://blog.hatena.ne.jp/ohbarye/","version":"1.0","width":"100%","description":"has_many*1\u306bblock\u3092\u6e21\u3059\u3068association\u306b\u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u308b\u3002 class User < ActiveRecord::Base has_many :posts do def stats group(:status).count # \u3053\u306econtext\u3067`self`\u306f`ActiveRecord::Associations::CollectionProxy` # `proxy_association.owner`\u3067`user.posts`\u306e`user`\u306b\u30a2\u30af\u30bb\u30b9\u3059\u308b\u3053\u3068\u3082\u3067\u304d\u308b end end end user.posts.stats # => {'draft'=\u2026"}