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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T20:08:52+00:00 2026-06-05T20:08:52+00:00

I have my mongoid query in the model def self.get_result collection_name, hash_params, page, per_page

  • 0

I have my mongoid query in the model

def self.get_result collection_name, hash_params, page, per_page 
  self.collection_name = collection_name
  @result_pg = self.where(hash_params).page(page).per(per_page)
end

I have it here because my collection names are passed as parameters and I don’t have models for all my collection. Instead I have one model and I set its name dynamically based on the request parameter.

My controller code

Collection.get_result params[:state], hash_param, params[:page], params[:per]

My View code

<%= paginate @result_pg %>

When I use the above code I get

undefined method `current_page' for nil:NilClass   ERROR
  • 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-06-05T20:08:53+00:00Added an answer on June 5, 2026 at 8:08 pm

    I think the variable is being lost when you call it in the model. Instead, have it return the object back to you like so:

    return self.where(hash_params).page(page).per(per_page) // put this in your model
    

    Then, I’m the controller, connect the pieces:

    @result_pg = Collection.get_result params[:state], hash_param, params[:page], params[:per] 
    // in your controller like so
    

    Right now, the controller isn’t setting @result_pg, that is why it is nil and you are getting the nil error. Try this and let me know if that worked out for you.

    Updated

    Ok, as for the reason this happens. When Rails loads a controller, the variables that you declare with a @ symbol, like @bologna will be passed to the view and something will be done with them.

    On the other hand, you are not technically declaring and instantiating that variable in the controller, you are doing it in the model, as per your source code that you posted above. Declaring the variable there can be done but it isn’t useful because the controller has no idea that it even happened. When you tell the Class Collection to perform a method, usually you have that method return something back to you, if you don’t, then the variable is lost.

    It is similar to me asking someone to go to the store and buy me some groceries, you did the right things, giving the method all the information it needed, passing it the right variables to do its job correctly, but not telling it to actually come back with them, is where the error occurs. In your method, the groceries get purchased at the store and left there. The revised function I wrote for you tells it return with the groceries and put them in the variable @result_pg. The variable is declared in the Controller like it is supposed to be.

    So in short, anything you want to have accessible in the view, needs to be declared in the controller. And anytime you want something back from a method, always have it return the information to you.

    Also, it isn’t entirely necessary to even have the method call to the Class Collection. In a project of my own where I use Kaminari, I just simply do the whole call from in the controller like so:

    @notes = current_user.notes.page params[:page]
    // My Application has :users that have_many :notes
    

    So you could simplify it that way if you want, but the method that I suggested that fixes it the way you are doing it will work too, however you prefer.

    I hope my explanation helped and wan’t too long winded.

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

Sidebar

Related Questions

I have in model post.rb : class Post include Mongoid::Document attr_accessible :content, :original_post end
I have the following structure: class User include Mongoid::Document end class Resource include Mongoid::Document
I have a model: class SimpleAction include Mongoid::Document field :set_date, :type => Date and
Using Mongoid 2.4.5 on Rails 3.2.1 I have a Model Book that has_many :pages
I have 2 models The first model category.rb class Category include Mongoid::Document # Relationships
I have following User model, embeds the Category model, class User include Mongoid::Document include
I have two models. I am trying to query a model for how many
I have a group model. I want to query a group to see if
I have a Mongoid model that looks like this: class V1Cache include Mongoid::Document field
If I have a model called Product class Product include Mongoid::Document field :product_id field

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.