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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T05:08:08+00:00 2026-05-23T05:08:08+00:00

I have an ActiveRecord object RaceCarDriver . Three of the fields are boolean: is_from_texas

  • 0

I have an ActiveRecord object RaceCarDriver. Three of the fields are boolean: is_from_texas, is_from_arkansas, and is_from_indiana. In the user search interface, the user could select neither to see all results, select is_from_texas to see only drivers from Texas, or select is_from_texas and is_from_indiana to see all drivers from one of those states.

Now, I know this example is a bit contrived, but I wanted to avoid the complexity of the actual app.

My best attempt is along these lines:

@drivers = RaceCarDriver.select('name').
where(params[:check_texas] == 0 || :is_from_texas => params[:check_texas]).
where(params[:check_arkansas] == 0 || :is_from_arkansas => params[:check_arkansas]).
where(params[:check_indiana] == 0 || :is_from_indiana => params[:check_indiana])

However, this ANDS the chained where clauses together, making it so that if Texas and Indiana were both checked a driver would have to be from both states.

Again, I know this is contrived. Any help is appreciated.

  • 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-23T05:08:08+00:00Added an answer on May 23, 2026 at 5:08 am
    @drivers = RaceCarDriver.select('name')
    @drivers = @drivers.where(:is_from_texas => params[:check_texas]) if params[:check_texas]
    @drivers = @drivers.where(:is_from_arkansas => params[:check_arkansas]) if params[:check_arkansas]
    @drivers = @drivers.where(:is_from_indiana => params[:check_indiana]) if params[:check_indiana]
    

    EDIT

    Solution 1:

    where_options = { :is_from_texas => params[:check_texas],
                      :is_from_arkansas => params[:check_arkansas],
                      :is_from_indiana => params[:check_indiana] }.select{|k,v| v.present? }
    where_conditions = where_options.map{|k,v| "#{k} = #{v}"}.join(" OR ")
    @drivers = RaceCarDriver.select('name').where(where_conditions)
    

    Solution 2:

    @scoped_drivers = RaceCarDriver.select('name')
    @drivers = []
    @drivers << @scoped_drivers.where(:is_from_texas => params[:check_texas]) if params[:check_texas]
    @drivers << @scoped_drivers.where(:is_from_arkansas => params[:check_arkansas]) if params[:check_arkansas]
    @drivers << @scoped_drivers.where(:is_from_indiana => params[:check_indiana]) if params[:check_indiana]
    @drivers.flatten!
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ActiveRecord object: @user = User.find_by_id(1) I want to access the name
I have an object class User < ActiveRecord::Base has_one :subscription end and I have
I have an ActiveRecord model whose fields mostly come from the database. There are
I have a basic ActiveRecord model in which i have two fields that i
I have an Activerecord object called Foo : Foo.attribute_names.each do |attribute| puts Foo.find(:all)[0].method(attribute.to_sym).call end
I have a hash object from an ActiveRecord. I'm going to be calling to_json
Let's say I have this object: [JsonObject] public class User { [JsonProperty(PropertyName = id)]
I have a simple ActiveRecord object representing an estimated time of arrival. class Eta
I have a complex table pulled from a multi-ActiveRecord object array. This listing is
I have a quite long method. It copy an ActiveRecord object with all relations,

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.