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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T07:11:55+00:00 2026-06-02T07:11:55+00:00

In order to avoid having to construct complicated dynamic SQL queries, I’d like to

  • 0

In order to avoid having to construct complicated dynamic SQL queries, I’d like to be able to just pass in nil values in my conditions, and have those ignored. Is that supported by ActiveRecord?

Here is an example.

event = Event.find(:all, :conditions => { 
                                          :title       => params[:title],
                                          :start_time  => params[:start_time], 
                                          :end_time    => params[:end_time]
                                        }).first

In that particular case, if params[:start_time] is set to nil, ActiveRecord will search for those Events that have their start_time set to null. Instead, I’d like it to just ignore start_time. How do I do that?

  • 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-02T07:11:57+00:00Added an answer on June 2, 2026 at 7:11 am

    You don’t have to “create complicated dynamic SQL queries” to do what you need. Simply construct your conditions hash separately, and either exclude the null values at the time of creation or after you’ve created the hash.

    conditions = {}
    conditions[:title] = params[:title] unless params[:title].blank?
    conditions[:start_time] = params[:start_time] unless params[:start_time].blank?
    conditions[:end_time] = params[:end_time] unless params[:end_time].blank?
    

    or

    conditions = {:title => params[:title], :start_time => params[:start_time], :end_time => params[:end_time]}
    conditions.delete_if {|k,v| v.blank? }
    

    or

    conditions = params.reject {|k,v| !([:title, :start_time, :end_time]).include?(k) }
    

    but that last form will only work if the keys are actually symbols. In Rails the params hash is a HashWithIndifferentAccess which allows you to access the text keys as symbols. Of course you could just use the text values in your array of keys to include if necessary.

    and then query with your pre-built conditions hash:

    event = Event.find(:all, :conditions => conditions).first
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

In order to avoid having to pass Data that goes in the master page
With Smarty 3.0, I would like to do, in order to avoid code repetition:
I have added the following option in order to avoid having smaller multiple binary
Can COMMON Table expressions be used to avoid having SQL Server perform the following
i am using a HashSet in order to avoid having two (or more)items with
In order to avoid soft delete I am creating a recycle bin database. The
I have never written copy constructor, so in order to avoid pain i wanted
In order to perform less api calls I would like to combine api calls
I recently asked another question pertaining to avoid having to use boilerplate exceptions when
Using T-SQL, I would like to execute an UPDATE statement that will SET columns

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.