-1

I want to select Users who own cats OR a fish as pet. there are users in my table who own both but right now I need only one or the other.

+----+------+------+

| id | cats | Fish |

+----+------+------+

| 1 | 0 | 1 |

| 2 | 1 | 0 |

| 3 | 1 | 1 |

| 4 | 0 | 1 |

| 5 | 1 | 0 |

Edit: This is needed in PSQL.

Ann
  • 139
  • 7
  • 2
    Possible duplicate of [ActiveRecord Arel OR condition](http://stackoverflow.com/questions/7976358/activerecord-arel-or-condition) – Brad Werth Oct 26 '15 at 17:14
  • ans is this: Foo.where('foo= ? OR bar= ?', 'a', 'b'). reference: http://stackoverflow.com/questions/3639656/activerecord-or-query – Ann Oct 27 '15 at 00:40

2 Answers2

0

The only way to make a WHERE X OR Y with ActiveRecord today is by writing a piece of raw SQL query in a where method :

User.where("cats > 0 OR Fish > 0")
Caillou
  • 1,451
  • 10
  • 19
0

If you didn't want to include owners with both fish and cats and you wanted a way to work across all databases, you can just use:

Owner.where("(cats > 0 AND fish = 0) OR (cats = 0 AND fish > 0)")

or

Owner.where("(cats > 0 OR fish > 0) AND (cats = 0 OR fish = 0)")

Bassel Samman
  • 802
  • 5
  • 11