#146 ✓resolved
RainChen

how to get the right count when using will_paginate and attachment_fu?

Reported by RainChen | November 2nd, 2007 @ 11:35 AM

I using attachment_fu for photo model, and the photos have some thumbnails with the parent_id relating to photo id.

The photo model looks like:

class Photo < ActiveRecord::Base

class << self

def find_all_recent(options = {})

default_options = { :conditions => {:parent_id => nil }, :order => "#{self.table_name}.created_at DESC" }

options = default_options.merge(options)

RAILS_DEFAULT_LOGGER.debug(options.inspect)

find(:all, options)

end

end

end

Then I use bellow to fetch the photos:

Photo.paginate_recent :page => 1, :per_page => 5

the logs come out:

SQL (0.000000) SELECT count(*) AS count_all FROM photos

{:conditions=>{:parent_id=>nil}, :offset=>0, :order=>"photos.created_at DESC"

, :limit=>15}

Photo Load (0.000000) SELECT * FROM photos WHERE (photos.`parent_id`

IS NULL) ORDER BY photos.created_at DESC LIMIT 0, 15

Notice that the count sql didn't use any conditions.

Then I use bellow:

class Photo < ActiveRecord::Base

class << self

def paginate_recent(options = {})

default_options = { :conditions => {:parent_id => nil }, :order => "#{self.table_name}.created_at DESC" }

paginate default_options.update(options)

end

end

end

then the count sql works as expected :

SQL (0.000000) SELECT count(*) AS count_all FROM photos WHERE (photo

s.`parent_id` IS NULL)

Photo Load (0.000000) SELECT * FROM photos WHERE (photos.`parent_id`

IS NULL) ORDER BY photos.created_at DESC LIMIT 0, 15

But this is quite not "DRY". There will be many Photo.paginate_xxx methods.

How to keep DRY ?

Comments and changes to this ticket

Please Sign in or create a free account to add a new ticket.

With your very own profile, you can contribute to projects, track your activity, watch tickets, receive and update tickets through your email and much more.

New-ticket Create new ticket

Create your profile

Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป

Everyone's favorite Ruby library for pagination of practically anything!

People watching this ticket

Pages