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

Sorry, you do not have permission to ask a question, You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please type your username.

Please type your E-Mail.

Please choose an appropriate title for the post.

Please choose the appropriate section so your post can be easily searched.

Please choose suitable Keywords Ex: post, video.

Browse

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

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Logo

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Navigation

  • Home
  • About Us
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • About Us
  • Contact Us
Home/ Questions/Q 4633

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise Latest Questions

Author
  • 61k
Author
Asked: November 27, 20242024-11-27T01:49:07+00:00 2024-11-27T01:49:07+00:00

How to Build OR Queries With Active Record

  • 61k

There are a couple of ways to recreate the SQL OR operator with Ruby on Rails and Active Record.

Here's how.

Checking a Single Column For Multiple Values

First, if you want to pull records where a certain column can be multiple values you won't need to use the OR SQL operator.

Instead, pass a list of the accepted values to Active Record and it will use the SQL IN operator.

Model.where(column: [1, 2, 3]) 
Enter fullscreen mode Exit fullscreen mode

This will generate SQL the looks something like this:

SELECT models.* FROM models WHERE (models.column IN (1,2,3)) 
Enter fullscreen mode Exit fullscreen mode

This pattern is extremely common and very practical. A couple of real examples:

User.where(role: [:admin, :mod])  BlogPost.where(tags: [ruby_tag, rails_tag])  comment_ids = [] # append selected comment ids Comment.where(id: comment_ids) 
Enter fullscreen mode Exit fullscreen mode


Checking Multiple Columns For Multiple Values

If we need to check multiple columns, we can't get away with using the SQL IN operator anymore.

Instead, we need to leverage the full strength and flexibility of OR queries.

Using the OR Operator in Rails 5+

Rails 5 introduced the OR condition in Active Record.

Here's an example:

Post.where(category: "featured").or(Post.where(promoted: true)) 
Enter fullscreen mode Exit fullscreen mode

Here's how to use it:

We break down our 2 acceptable conditions into individual queries. In this case, we want to fetch all the posts with a category set to “featured” and then also pull all the posts that have their promoted field set to true.

Post.where(category: "featured") Post.where(promoted: true) 
Enter fullscreen mode Exit fullscreen mode

You then call Active Record's .or method on the first query, and pass it the second query as an argument.

Here's the SQL output you would get:

SELECT * FROM posts WHERE (category = 'featured') OR (promoted = true) 
Enter fullscreen mode Exit fullscreen mode

That's it! You'll get results the match either condition in your query.

Using the OR Operator in Rails 4 and Below

Earlier versions of Rails don't have support for OR queries in Active Record, but you can still pass raw SQL to your where conditions.

Here's how that would look:

Post.where("category = ? or promoted = ?", "featured", true) 
Enter fullscreen mode Exit fullscreen mode

If you're curious about the ? syntax above, this is a technique to protect your database against SQL injection attacks. More on that next.


Preventing SQL Injection Attacks

It would be irresponsible to not give a quick overview of how you might expose yourself to SQL injection attacks.

If you're not familiar, it's a vulnerability where user input is passed directly into your database queries. If you unintentionally allow this, the user can purposely input malicious SQL code into the input. And that SQL will be run on your database.

If we don't substitute variables in our query and instead pass them directly, we might expose our entire database to the user.

It works like this.

When you pass a variable to a where clause directly, it will pass the variable to the database as-is.
If the user has malicious intent, they can pass unescaped strings directly to your database and wreak havoc.

Don't do this:

Post.where("category = #{params[:category]}") 
Enter fullscreen mode Exit fullscreen mode

You can't guarantee the input you get from params[:category] is safe for your database.

When you pass strings as the second argument in the where method, Active Record does the proper escaping needed to protect your database.

Better example:

Post.where("category = ?", params[:featured]) 
Enter fullscreen mode Exit fullscreen mode


Final Words

Active Record is super powerful and gives you multiple ways to construct your OR query.

A quick note on the SQL outputs though – what you see here should be similar to what your Rails app generates, but it will depend on the database adapter your app is using. Different databases have subtle differences and unfortunately, they aren't all exactly the same. But this is why we have Active Record! It abstracts away that complexity and gives a simple way to interface with any database.

programmingrailsrubywebdev
  • 0 0 Answers
  • 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

Sidebar

Ask A Question

Stats

  • Questions 4k
  • Answers 0
  • Best Answers 0
  • Users 1k
  • Popular
  • Answers
  • Author

    How to ensure that all the routes on my Symfony ...

    • 0 Answers
  • Author

    Insights into Forms in Flask

    • 0 Answers
  • Author

    Kick Start Your Next Project With Holo Theme

    • 0 Answers

Top Members

Samantha Carter

Samantha Carter

  • 0 Questions
  • 20 Points
Begginer
Ella Lewis

Ella Lewis

  • 0 Questions
  • 20 Points
Begginer
Isaac Anderson

Isaac Anderson

  • 0 Questions
  • 20 Points
Begginer

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help

Footer

Querify Question Shop: Explore Expert Solutions and Unique Q&A Merchandise

Querify Question Shop: Explore, ask, and connect. Join our vibrant Q&A community today!

About Us

  • About Us
  • Contact Us
  • All Users

Legal Stuff

  • Terms of Use
  • Privacy Policy
  • Cookie Policy

Help

  • Knowledge Base
  • Support

Follow

© 2022 Querify Question. All Rights Reserved

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.