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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T07:05:40+00:00 2026-05-15T07:05:40+00:00

I am using search logic to filter results on company listing page. The user

  • 0

I am using search logic to filter results on company listing page. The user is able to specify any number of parameters using a variety of named URLs. For example:

/location/mexico
/sector/technology
/sector/financial/location/argentina

Results in the following respectively:

params[:location] == 'mexico'
params[:sector] == 'technology'
params[:sector] == 'financial' and params[:location] == 'argentina'

I am now trying to cleanup or ‘DRY’ my model code. Currently I have:

def self.search(params)
    ...
    if params[:location]
        results = results.location_permalink_equals params[:location] if results
        results = Company.location_permalink_equals params[:location] unless results
    end
    if params[:sector]
        results = results.location_permalink_equals params[:sector] if results
        results = Company.location_permalink_equals params[:sector] unless results
    end
    ...
end

I don’t like repeating the searchs. Any suggestions? Thanks.


  • 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-15T07:05:41+00:00Added an answer on May 15, 2026 at 7:05 am

    This is how I would write it:

    [params[:location], params[:sector]].reject(&:nil?).each do |q|
      results = (results ? results : Company).location_permalink_equals q
    end
    

    There’s plenty of other ways, just an idea. Has the benefit of making it easy to add say params[:street] or something.

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

Sidebar

Related Questions

I'm using search logic to filter and order my results but it removes records
I'm using the search filter (objectClass=user) to find user objects, but of course it
I'm using a search core results and using xsl to filter the results based
I've been using the search function but I think there isn't any related question
I'm using the code below for my search logic, basically, it evaluates a field
Am working on web based Job search application using Lucene.User on my site can
I am using Coldfusion, to do a site search for my company. I have
I’m using Kentico v7. I’m using the Smart Search Results widget and I’m using
I am using Searchlogic to perform searches on my 'report' index page. <% form_for
I am using Microsoft Search Server 2008 + C# + .Net 3.5 + VSTS

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.