A "user" has many "posts". I want to create a counter_cache in "users" that shows how many posts they have.
This is straightforward to do - but I only want posts to be counted in the counter_cache if they are public (i.e post.is_public === true
).
How can I create a column in the users table that has their total number of public posts?
class User < ActiveRecord::Base
// Columns: id:integer name:string
has_many :posts
end
class Post < ActiveRecord::Base
// Columns: id:integer user_id:integer is_public:boolean content:text
belongs_to :user, dependent :destroy, counter_cache: true
end