0

Say I have a hash like so

attributes = {"brown" => true, "black" => false, "rocky" => true, "pebbles" => false, "beige" => true, "white" => false, "red" => true, "pink" => "true" }

And I have an model Beach where I want to query for the properties in the hash

Beach.where(attributes)

I believe this joins the hash attributes with the AND clause brown is true AND black is false AND rocky = true.

I need to join the hash attributes with the OR condition brown is true OR black is false OR rocky is true. Any Ideas? Thank you!

blee908
  • 12,165
  • 10
  • 34
  • 41
  • Take a look at: http://stackoverflow.com/questions/7976358/activerecord-arel-or-condition – cthulhu Sep 04 '14 at 18:48
  • Not sure if that exactly answers my question, I have a "attributes" hash with a variable number of attributes I want to merge with "OR" in the where clause. – blee908 Sep 04 '14 at 19:09
  • 1
    It does not exactly answer your question, so I didn't post it as an answer :) The article I've mentioned provides enough information to solve your problem, unless you are too lazy to try. – cthulhu Sep 04 '14 at 19:13
  • `arel` can do that. I got it to perform similar queries. You need to get an Arel table, assemble an array of conditions and inject them with `:or`. Typing code on a phone is not too handy, sorry. – D-side Sep 04 '14 at 20:44
  • I'm not looking to install anything new like `arel`. Is there anyone I can do this with plain Ruby/ Rails? – blee908 Sep 04 '14 at 21:21
  • @bennett check this answer http://stackoverflow.com/a/5000839/1081668 – Thanh Sep 05 '14 at 10:39
  • The answer extracts the individual attributes. I have about 100 column of attributes so I would prefer not extracting them individually. – blee908 Sep 05 '14 at 23:10

0 Answers0