0

I'm used scope where(address: [nil, ''], city: [nil, '']) and I get request

WHERE ((`address`.`address` = '' OR `address`.`address` IS
NULL) AND (`address`.`city` = '' OR `address`.`city` IS
NULL))

I need replace AND to OR. How do I build a query using ActiveRecord?

Andy
  • 863
  • 1
  • 8
  • 20

2 Answers2

0

You can try this

where("address.address in (?) or address.city in (?)", [nil, ''])

In application

scope :empty_address, -> { where("address.address in (?) or address.city in (?)", [nil, '']) }
Address.empty_address
Rokibul Hasan
  • 4,078
  • 2
  • 19
  • 30
0

You can use Arel gem and form you query like:

ids = [nil,''] Address.where(Address.arel_table[:address].in(ids).or(Address.arel_table[:city].in(ids)))

with Arel-helpers you can make it even shorter: Address.where(Address[:address].in(ids).or(Address[:city].in(ids)))