Skip to content

Instantly share code, notes, and snippets.

@jcsrb
Created December 10, 2011 12:33
Show Gist options
  • Save jcsrb/1455060 to your computer and use it in GitHub Desktop.
Save jcsrb/1455060 to your computer and use it in GitHub Desktop.
usefull scopes for Rails3
#include in your model
#include Scopes
module Scopes
def self.included(base)
#scope for NOT IN
#usage:
# Model.not_in(1)
# Model.not_in(1,2,4)
# Model.not_in([1,2,4])
# Model.not_in(1,2,3,[4,5,6])
# Model.not_in(Model.where(:attribute => value))
# Model.not_in(model_instance)
# Model.not_in(Model.where(:atrribute => value),model_instance)
# any other combinations
base.scope :not_in, lambda { |*a| where("`#{base.name.tableize}`.`id` NOT IN (#{a.flatten.collect{|el| el.is_a?(Integer) ? el: el.id}.join(',')})") }
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment