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 ?