views:

254

answers:

3

Hy,

My code:

@profile.images

and i would like to get only 10 images at time and with a 10 offset, like this

@profile.images(:limit => 10, :offset => 10)

and not like this

has_many :images, :limit => 10, :offset => 10

Then I would like to count in someway all the images for that profile.

@profile.count_images

Thanks (:

+1  A: 

You can use with_scope to scope your call to @profile.images, and perform the count outside the scope.

Image.with_scope(:find => { :limit => 10, :offset => 10 }) do
  @profile.images      # load association using limit and offset
end

@profile.images.reset  # reset cached association, else size would return <=10
@profile.images.size   # go to the database again for a real COUNT(*)
vladr
Thanks. You gave me a new idea: named_scope :limit, lambda { |num| { :limit => num } } named_scope :offset, lambda { |num| { :limit => num } }and then @profile.images.limit(10).offset(10)
Totty
+1  A: 

Use association extensions:

class Profile < ActiveRecord::Base
  has_many :images do
    def page(limit=10, offset=0)
      all(:limit=> limit, :offset=>offset)
    end
  end
end

Now you can use the page method as follows:

@profile.images.page # will return the first 10 rows
@profile.images.page(20, 20) # will return the first 20 rows from offset 20
@profile.images # returns the images as usual

Edit

In this specific case, association function might be a suitable option. Even lambda with named_scope might work. If you define it on the Profile class you are loosing the reusable aspect of the named_scope. You should define the named_scope on your image class.

class Image < ActiveRecord::Base

  named_scope :paginate, lambda { |page, per_page| { :offset => ((page||1) -1) * 
                              (per_page || 10), :limit => :per_page||10 } }

end

Now you can use this named_scope with the association:

@profile.images.paginate(2, 20).all

Or you can use the named_scope directly on the Image class

Image.paginate(2, 20).all(:conditions => ["created_at > ?" , 7.days.ago])

On the other hand why are not using the will_paginate plugin?

KandadaBoggu
Thanks, this is a lot better. I got this, but is not soo cool: named_scope :paginate, lambda { |page, limit, offset| {:limit => limit || 10, :offset => offset || ((limit || 10) * ( page - 1 ) )}}thanks really! now i will try it out ;)
Totty
+1 for a more advanced technique
vladr
I have updated my answer with a named_scope solution.
KandadaBoggu
Yes, maybe is better to use the will paginate. I will go to learn some, right now. Anyway, thanks for this. It could help for other things...
Totty
A: 
has_many :images, :foreign_key => 'on_id', :conditions => 'on_type = "profile"' do
def paginate(page = 1, limit = 10, offset = nil)
  page = nil if page < 1
  limit = 1 if limit < 1
  offset = 0 if(offset && offset < 0)
  offset = 0 if (!page)
  offset = limit * (page - 1) if (page)

  all(:limit=> limit, :offset => offset)
end

end

Now I would like to add this behaviour to other has_many relationships. But I would not like to copy paste the code... Any idea? :P

Totty
Add the method to a module. In your `has_many` association use the `:extend` option, i.e. `:extend => AbcModule`.
KandadaBoggu