Skip to content
Find file
Fetching contributors…
Cannot retrieve contributors at this time
50 lines (31 sloc) 2.47 KB

Gotcha!

I'm working late on a Saturday at the office on a Rails project (I have a real first world problem). One of the neat things you probably already know about ActiveRecord::Relation is that you can create something that looks like a scope using a class method. Here's one that duplicates the .find(...) method, more or less:

class Example < ActiveRecord::Base

  def self.including_ids(ids)
    where(id: ids)
  end

end

This takes advantage of the fact that you can provide an array as a value and ActiveRecord::Relation will generate an WHERE id IN (...) clause for you. Alas, I needed to write my own clause to achieve the reverse:

class Example < ActiveRecord::Base

  def self.including_ids(ids)
    where(id: ids)
  end

  def self.excluding_ids(ids)
    where('id NOT IN (:exluded_ids)', exluded_ids: ids)
  end

end

And the gotcha? Well what do you think happens when you write Example.including_ids([]). If you said "an empty selection," you're right. What about this?

Example.excluding_ids([])
  => # ????

Were you expecting an empty selection? Me neither.


My recent work:

JavaScript AllongéCoffeeScript RistrettoKestrels, Quirky Birds, and Hopeless Egocentricity


(Spot a bug or a spelling mistake? This is a Github repo, fork it and send me a pull request!)

Reg Braithwaite | @raganwald

Jump to Line
Something went wrong with that request. Please try again.