This question is an offshoot from the HABTM Associations in Rails: collecting and counting the categories of model children .
Given:
class Category < ActiveRecord::Base
has_and_belongs_to_many :books
validates_uniqueness_of :name
end
class Book < ActiveRecord::Base
has_and_belongs_to_many :categories
end
class Store < ActiveRecord::Base
has_many :books
has_many :categories, through: :books
end
Task:
In the store window, indicate the number of books for each category.
Store.first.books_per_category
desired result:
[ { name: 'mystery', count: 5 }, { name: 'fantasy', count: 6 } ]
However, each store can have a huge number of books and categories .
I am trying to create a single execution request that receives only a column of names and the number of books for each individual category associated with the Store, without loading the books into memory.
I have already tried:
class Store < ActiveRecord::Base
def books_per_category
categories.eager_load(:books).map do |c|
{
name: c.name,
count: c.books.size
}
end
end
def books_per_category2
categories.distinct.map do |c|
{
name: c.name,
count: c.books.count
}
end
end
end
Database Schema:
ActiveRecord::Schema.define(version: 20150508184514) do
create_table "books", force: true do |t|
t.string "title"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "store_id"
end
add_index "books", ["store_id"], name: "index_books_on_store_id"
create_table "books_categories", id: false, force: true do |t|
t.integer "book_id", null: false
t.integer "category_id", null: false
end
add_index "books_categories", ["book_id", "category_id"], name: "index_books_categories_on_book_id_and_category_id"
add_index "books_categories", ["category_id", "book_id"], name: "index_books_categories_on_category_id_and_book_id"
create_table "categories", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "stores", force: true do |t|
t.string "name"
t.datetime "created_at"
t.datetime "updated_at"
end
end