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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T18:14:21+00:00 2026-06-15T18:14:21+00:00

I have a code structure like this: if params[:one] && params[:two] object = Something.where(:one

  • 0

I have a code structure like this:

if params[:one] && params[:two]
  object = Something.where(:one => params[:one], :two => params[:two])
elsif params[:one]
  object = Something.where(:one => params[:one])
elsif params[:two]
  object = Something.where(:two => params[:two])
else
  object = Something.all
end

Basically, two optional parameters in the URL that filter the query to be made. Can this be cleaner?

  • 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-15T18:14:22+00:00Added an answer on June 15, 2026 at 6:14 pm

    I’d write:

    objects = Something.where(params.slice(:one, :two))
    

    Notes:

    1. Usually an empty param is also to be considered as non-existing. In this case: Something.where(params.slice(:one, :two).select { |k, v| v.present? }).

    2. Strange as it may look, in your snippet you should write object = if ..., it’s far more idiomatic being Ruby a language where conditionals are expressions and not statements.

    3. Don’t call all as a no-op, you don’t have a ActiveRecord::Relation (cool) anymore, but a Array (not so cool). where({}) is perfectly fine.

    4. Naming is very important! If you see object you think “single element” (which is not true here), if you see objects you think “collection”.

    5. If the param keys does not match DB columns: a lot of ways to do it, for example:

      pairs = {:x => :db_x, :y => db_y}.map { |k, v| [v, params[k]] if params[k] }]
      Something.where(Hash[pairs.compact])
      

    Or being just two attributes:

    attrs = {:db_one => params[:one], :db_two => params[:two]}
    Something.where(attrs.select { |k, v| v })
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a project with a structure like this: project code art config Art
let's say I have a html structure like this -> (note: in my code
Is it possible in Rails to have code like this? if ... skip_to_section_1 end
hi have a code structure like this public static $userId; public static $checkUserId; public
ok so i have code that fundementally has this structure: firstly a main div
I have the following SVN structure: /Project1 /trunk ( Shared code between all clients
I have two tables with same structure **t1** +------+------+ | code | vid |
I have an XML doc with a structure like this: <Book> <Title title=Door Three/>
I have this code which reads all the files from a directory. File textFolder
I have a folder structure like this: package_name/ |-- module1 |-- module2 |-- module3

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.