How to Use Where Method In Rails

In Rails, you can query the database through ActiveRecord methods to access table data.

And this one ActiveRecord methods is Where, today we are learn multiple ways to use Where method in rails.

If you want to fetch multiple data into one single query then use where method.

ActiveRecord gives you a number of ways to do just that with the where method.

Rails: How to use String in where method

A single string, without additional arguments use like this,

Where: Array with AND & OR condition

In array style you can write multiple column and get results with and & or condition.

you can do it like this

Alternatively, you can use named placeholders in the array, and pass a hash as the second element of the array.

Where will also accept a hash condition, columns can be symbols or strings, values can be single values, arrays, or ranges.

Where: Rails join association

Joins use in where is basically fetch association table data into one query, In rails joins default fetch INNER JOIN query here we see multiple way to use where in join association

do it like this,

How to Use Where Not Condition

This is quite complex query!

I personally not use much but If you want to check that condition is NOT true, then you can combine where with the not method.

Try it yourself……

Thank you for reading this blog πŸ™πŸ»

I hope it will be more helpful to you!