Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

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

#146

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 ?

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

State: resolved
Milestone: none
Assigned to: Mislav Mislav

Activity

  1. Mislav
    Mislav
    • State changed from new to resolved

    see my comments on #66

    November 5th, 2007 @ 10:50 AM

  2. RainChen
    RainChen

    I think your suggestion is my solution which I posted here.

    But I find that the best solution may be the has_finder plugin. I will try it.

    November 9th, 2007 @ 08:00 AM

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.