I have a number of objects which are associated together, and I'd like to layout some dashboards to show them off. For the sake of argument:
- Publishing House - has many books
- Book - has one author and is from one, and goes through many states
- Publishing House Author - Wrote many books
I'd like to get a dashboard that said:
- How many books a publishing house put out this month?
- How many books an author wrote this month?
- What state (in progress, published) each of the books are in?
To start with, I'm thinking some very simple code:
@all_books = Books.find(:all, :joins => [:author, :publishing_house], :select => "books.*, authors.name, publishing_houses.name", :conditions => ["books.created_at > ?", @date])
Then I proceed to go through each of the sub elements I want and total them up into new arrays - like:
@ph_stats = {}
@all_books.map {|book| @ph_stats[book.publishing_house_id] = (@ph_stats[book.publishing_house_id] || 0) + 1 }
This doesn't feel very rails like - thoughts?