This question is an offshoot from HABTM associations in Rails : collecting and counting the categories of a model's children.
class Cate
You'll want to be creating a method (or scope) on the Categories object so something like.
Category.joins(:books).select('categories.*, COUNT(books.id) as book_count').group('categories.id')
the resulting object will now have every attribute of an instance of category and respond to a method, book_count which returns the number of books with that instances category id.
Its noteworthy to mention this will omit any categories that do not have books associated with them. if you want to include those, the query needs to be updated to the following:
Category.joins('LEFT OUTER JOIN books_categories on books_categories.category_id = categories.id').select('categories.*, COUNT(books_categories.book_id) as book_count').group('categories.id')