>
> It seems as though they've done away with the 'paginate' command in
> Rails 2. The result is that the following code:
> <i> @book_pages, @books = paginate :books, per_page=>10,
> include => [:publishers, :authors] </i>
> no longer works.
It's moved into a plugin, but you already found that out :-)
>
>
> To get round this, I just change my code to list whatever books are in
> the database and forget about pagination all together:
>
> <i> @books = Book.find :all, <b>:include =>
> [:publisher, :authors] </b> </i>
>
>
> My question regards the use of the code - :include =>
> [:publishers, :authors], (seen in bold above).
>
> The book tells us that this code is needed and used by the 'paginate'
> command to help cut down on SQL activity. Does this code still do
> it's job now that I've removed the paginate command?
Yes, it does (and it has nothing to do with pagination, as you'll see
in a minute).
> It doesn't seem
> to cause throw up any exceptions but I'm not convinced it's saving me
> any databse calls.
It's called eager loading, meaning it uses a join to get not only the
books, but also their respective publishers and authors in the same
sql call.
If you don't use eager loading, the one call in the controller will
fetch all the n books. Then in the view you want to display
information about the book publisher and authors. This results in two
additional queries (one for publisher and one for authors) for each
book. The total number of queries in this case is thus 2n+1 instead of
only 1 with eager loading. With large numbers of objects the
difference in speed can be huge.
(This all is explained in the book both on page 117 and 399 ;-)
Note that in the latest edge Rails, the inner workings of eager
loading has changed a bit so it might cause three queries instead of
one. This is because with very large datasets join queries tend to
also get slow and you can mitigate that somewhat by fetching (all) the
authors and publishers in separate (single) queries. But this is
cutting edge, something like days old.
>
>
> How do I find out if it is helping on the SQL side of things and
> furthermore, what can I do about it if it isn't helping. Anyone got
> any handy tips for getting round this problem?
Watch the logs. If you're in development environment, logs/
development.log will faithfully log all activity during a request.
Cheers,
//jarkko
--
Jarkko Laine
http://jlaine.net
http://dotherightthing.com
http://www.railsecommerce.com
http://odesign.fi