1

I've been using the following syntax when querying in my rails project.

Table.where({ fld_1: "val_1", fld_2: "val_2" })

which will generate the following sql

SELECT * FROM tables WHERE fld_1 = 'val_1' AND fld_2 = 'val_2'

However, I want to make a query using or not and as conditional opoerator. Ex SELECT * FROM tables WHERE fld_1 = 'val_1' or fld_2 = 'val_2'

I can attain it using this query

Table.where(["fld_1 = ? and fld_2 = ?", "val_1", "val_2"])

But how can I achieve it using the Hash style of querying.

Lymuel
  • 574
  • 3
  • 10

1 Answers1

0

According to Rails documentation on Hash querying:

Only equality, range and subset checking are possible with Hash conditions.

So your best bet would be to skip the hash and use an Array like you mentioned:

Table.where('fld_1= ? OR fld_2= ?', 'val_1', 'val_2')

Fortunately, or conditions similar to what you're looking for will be coming in Rails 5 (which has been backported to Rails 4.2.2). When it comes out you will be able write your query like this:

Table.where(fld_1: 'val_1').or.where(fld_2: 'val_2')
Community
  • 1
  • 1
Kelsey Hannan
  • 2,857
  • 2
  • 30
  • 46