0

In Rails, how do you write the ActiveRecord, "where this column's value is not equal to any value in this array of strings"?

I'm trying to move some ruby logic into SQL by rewriting a .reject method into a .where.

# old
SomeModel.all.reject{ |sm| some_array.include? sm.x } # works, but is inefficient

# new
SomeModel.where(__________) # what goes here?

EDIT EDIT EDIT

By the way, I was initially having problems because I didn't understand how SQL handles NULLs in IN. This SO question explains it nicely.

Community
  • 1
  • 1
thewillcole
  • 2,943
  • 3
  • 30
  • 35

1 Answers1

2

What about this?

SomeModel.where('sm not in :arr', {arr: some_array})
Sergio Tulentsev
  • 226,338
  • 43
  • 373
  • 367
  • Thanks, Sergio. That worked once I removed the `nil` value from my `some_array` -- as noted in the EDIT to the question above. – thewillcole Mar 15 '12 at 23:30