Let’s say I’ve got the following models:
Book
MyBook
User
Author
Where books and user has_many :through => my_books, author has_many
books, and books belongs_to author and user.
If books/index showed me all of the books with each author’s name, I
could use eager loading like so:
@books = Book.find(:all, :include => :author)
So that when I iterate through a hundred books and authors, I’ve
already loaded the author. If I want to do the same, but with my_books/
index, I don’t know how to include :author, b/c it looks like this:
@books = current_user.books
I’ll get all the the books, but I’ll have to load all the authors for
each song. The same is true if I were to use:
@books = MyBook.find_all_by_user_id(current_user, :include => song)
Which means that when I’m iterating through books, I have to hit the
database every for every book to get its author. Is there any way to
use eager loading for models once removed, like author is in this
example? (i.e., User -> Book -> Author or MyBook -> Book -> Author)
Hopefully I got that all across. Is there a better way?