Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 704989
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T04:00:13+00:00 2026-05-14T04:00:13+00:00

Hy, My code: @profile.images and i would like to get only 10 images at

  • 0

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


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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-14T04:00:13+00:00Added an answer on May 14, 2026 at 4:00 am

    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 you not using the will_paginate plugin?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am using below code to get profile image of friends using Resfb. I
I try to update my profile image and use this code: $connection->post('account/update_profile_image', array('image' =>
I would like to display the users profile picture inside of my applications canvas
I would like to provide a default profile picture for a user. I've used
I have something that I would like to do, but I am not sure
I would like to add elements to the three ArrayLists declared in the code
I need to profile my JAVA SRC code for some particular problem I am
I implemented a custom Profile object in code as described by Joel here: How
i'm trying to profile a quicksort code. the code is as follows: qsort []
I want to profile my Perl code by using Devel::DProf . when I am

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.