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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T23:48:23+00:00 2026-06-17T23:48:23+00:00

How can I search/filter by multiple parameters that are each optional in Rails 3.2?

  • 0

How can I search/filter by multiple parameters that are each optional in Rails 3.2? With the setup below, I am currently getting the following error. Any help is greatly appreciated.

undefined method `paginate' for nil:NilClass

Application Trace | Framework Trace | Full Trace
app/controllers/contacts_controller.rb:50:in `index'

Here is the index action in my contacts_controller:

def index
  city = params[:city]
  state = params[:state]
  zip = params[:zip]
  @contacts = Contact.search(city,state,zip).paginate(:page => params[:page], :per_page => items_per_page)
end

And here is the search method in my Contact model:

def self.search(city, state, zip)
  joins(:profile => :addresses)
  .where("city like ?", "%#{city}%") unless city.blank?
  .where("state = ?", state) unless state.blank?
  .where("zip like ?", "%#{zip}%") unless zip.blank?
end
  • 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-17T23:48:25+00:00Added an answer on June 17, 2026 at 11:48 pm

    Try to return more explicitely, e.g. like that:

    def self.search(city, state, zip)
      query_obj = joins(:profile => :addresses)
      query_obj = query_obj.where("city like ?", "%#{city}%") unless city.blank?
      query_obj = query_obj.where("state = ?", state) unless state.blank?
      query_obj = query_obj.where("zip like ?", "%#{zip}%") unless zip.blank?
    
      query_obj
    end
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to write a php code that can search for multiple keywords separated
How can I use a a search filter to display users of a specific
How can I do a Filter across multiple fields in Lucene.Net? On one field
I'm aware that I can search queries by calling where() on a model as
I have a form that I used to filter search results that consist of
I can't seem to understand the QueryDSL for facets in elastic search. Below are
how can i search and filter close string in listview. for example i have
I have a search function that can quite happily search through a list divs
I'm putting together a search function that searches through multiple data attributes on a
Can search engines such as Google index JavaScript generated web pages? When you right

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.