For example, say I have something like BlogCategory which has a HABTM with BlogPost, and I want to select only the BlogCategories that have actually been used in a BlogPost
named_scope :published, {
:include => :blog_posts,
:select => 'blog_categories.*, count(blog_posts.id) as post_count',
:group => 'blog_categories.id having post_count > 0',
:conditions => 'blog_posts.published = 1',
}
Problem I’m having is that the :select part of this seems to be getting completely ignored by rails, so the count field doesn’t get put into the query, and I end up with the error “Unknown column ‘post_count’ in ‘having clause'”
I don’t know why it doesn’t recognize post_count but it should work with: