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 5941413
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T16:06:23+00:00 2026-05-22T16:06:23+00:00

On my Question model I have some scopes scope :recent, order(created_at DESC) scope :approved,

  • 0

On my Question model I have some scopes

scope :recent, order("created_at DESC")  
scope :approved, where("status = ?", "approved")       
scope :answered, approved.recent.where("answers_count > ?", 0)

On my question controller I’m retrieving questions using the scopes

example 1:

@questions = Question.approved.recent

example 2:

@questions = User.find(session[:user_id]).topics.map { |t| t.questions.approved.recent }.flatten.uniq

I’m trying to put will_paginate on my model to make things easier on the controller but the 2nd example is very tricky as it is using mapping to retrieve questions according to preferences.

I’ve tried to add this on my model

  def self.pagination(page = 1)
      self.paginate(:page => page, :per_page => 5)
    end

and then on my controller I have

@questions = Question.approved.recent.pagination.(params[:page])

That works fine for the 1st example but I Dont know how to implement that on the 2nd example

Any hints?

  • 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-22T16:06:24+00:00Added an answer on May 22, 2026 at 4:06 pm

    This looks like Rails 3. Be sure to use the ~> 3.0.pre2 version of the will_paginate gem.

    You can use the paginate method at the end of your chain of scopes. For example, your “example 1” would be:

    @questions = Question.approved.recent.paginate(:page => params[:page], :per_page => 20)
    

    I see you created a custom method (pagination) to wrap this pattern, but it’s best that you keep this syntax in original form for now, especially since you’re dealing with scopes and Relation objects in Rails 3 and will_paginate doesn’t have proper support for this yet (but it’s coming).

    In your “example 2” it seems you only need to fetch the first few recent questions from each topic and that you won’t perform a full-blown pagination here (like, going to page 2 and forward). You don’t have to use the paginate method here; you can simply use ActiveRecord’s limit:

    current_user = User.find(session[:user_id])
    @questions = current_user.topics.map { |topic|
      topic.questions.approved.recent.limit(5).to_a
    }.flatten.uniq
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A simple question: I have a Model-View-Controller setup, with Models accessing a SQL database.
I have some basic questions of calling a Fortran model from GAE. I uploaded
I have a model Question with a field called userid , before one ask
I have a django app with models as follows: A Question model An Answer
I have a question about Android Market/Google Play license model for paid applications. First
I have a simple question regarding accessing member variables of a model object. I
I have a question about the proper, best way to manage the model. I
I have one question. I am using Apache CXF framework to generate model classes
Dynamic Data question: I have 2 fields of type Nullable<DateTime> on my model When
What I have: QTreeView class with table data And connected QAbstractTableModel model Question :

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.