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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 13, 20262026-05-13T15:40:32+00:00 2026-05-13T15:40:32+00:00

I have a an method that retrieves Groups that are present in certain areas.

  • 0

I have a an method that retrieves Groups that are present in certain areas. Groups are given a country_id, region_id and city_id

The UI gives three select boxes to choose a country, a region from that country and then a city from that region. To find all groups in a particular city, I have this code:

@groups = Group.find(:all, :conditions => {:city_id => params[:city_id]})

This all works fine, but I also want it to find all groups in an area when the lower criteria isn’t specified. For example, If a country and region are given, but not city, I’d like to find it by the region.

What I’m doing is this:

if !params[:city_id].nil?
    @groups = Group.find(:all, :conditions => {:city_id => params[:city_id]})
else
    if !params[:region_id].nil?
         @groups = Group.find(:all, :conditions => {:region_id => params[:region_id]})
    else
         @groups = Group.find(:all, :conditions => {:country_id => params[:country_id]})
    end
end

This works perfectly well, but it seems like it’s a little inefficient. Am I doing it the best way or can I streamline a little?

One idea I had was to have a single find checking against all parameters, but I could not work out how to effectively ‘ignore’ parameters that were nil – my main thought was to check which ones were not set and set them to something like ‘*’ or ‘true’, but that’s not how SQL plays the game.

  • 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-13T15:40:33+00:00Added an answer on May 13, 2026 at 3:40 pm

    Sounds like a job for named scopes:

    class Group < ActiveRecord::Base
      named_scope :in_city, lambda { |city_id| {
        :conditions => { :city_id => city_id }
      }}
    
      named_scope :in_region, lambda { |region_id | {
        :conditions => { :region_id => region_id }
      }}
    
      named_scope :in_country, lambda { |country_id | {
        :conditions => { :country_id => country_id }
      }}
    end
    

    This establishes some simple scopes for restricting the Group records. Presumably you have indexed your database properly so these are quick to resolve.

    The controller is much easier to implement then:

    def index
      @group_scope = Group
    
      if (!params[:city_id].blank?)
        @group_scope = @group_scope.in_city(params[:city_id])
      elsif (!params[:region_id].blank?)
        @group_scope = @group_scope.in_region(params[:region_id])
      elsif (!params[:country_id].blank?)
        @group_scope = @group_scope.in_country(params[:country_id])
      end
    
      @groups = @group_scope.all
    end
    

    Generally you should be testing for .blank? instead of .nil? as some form elements can send in empty results, such as a select with something akin to “All” as the default.

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

Sidebar

Related Questions

I have a WCF service method that expects an object and then retrieves its
So I have a php file that retrieves some variables with the $_GET method
I have method that returns module path of given class name def findModulePath(path, className):
I have method in my BLL that interacts with the database and retrieves data
I have a method that retrieves data from a json serialized string and writes
I have data access method that retrieves xml from database and returns back to
I have a php method that creates an HTML table with data it retrieves
I have a DAL method that retrieves items. The items exist in the DAL
I have a method that calls a service to retrieve an instance of an
I have method that returns Drawable , and if its Bitmap object is recycled

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.