Pagination Deprication issues in rails 2.0

0 views
Skip to first unread message

stephenjamesmurdoch

unread,
Apr 20, 2008, 5:36:24 PM4/20/08
to Beginning Ruby on Rails E-Commerce
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.

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? It doesn't seem
to cause throw up any exceptions but I'm not convinced it's saving me
any databse calls.

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?

Regds
Steve

stephenjamesmurdoch

unread,
Apr 20, 2008, 6:36:13 PM4/20/08
to Beginning Ruby on Rails E-Commerce
Problem resolved:

./script/plugin install svn://errtheblog.com/svn/plugins/classic_pagination

seems to fix everything.

:)

+e00


Jarkko Laine

unread,
Apr 21, 2008, 2:17:37 AM4/21/08
to railsec...@googlegroups.com
On 21.4.2008, at 0.36, stephenjamesmurdoch wrote:

>
> 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


captproton

unread,
Apr 21, 2008, 11:56:35 PM4/21/08
to Beginning Ruby on Rails E-Commerce
Jarkko, should this be updated on github?
> smime.p7s
> 3KDownload

stephenjamesmurdoch

unread,
Apr 26, 2008, 2:00:31 PM4/26/08
to Beginning Ruby on Rails E-Commerce
Hi Jarko,

Thanks a lot for the tips :)
steve
Reply all
Reply to author
Forward
0 new messages