Created
December 10, 2011 12:33
-
-
Save jcsrb/1455060 to your computer and use it in GitHub Desktop.
usefull scopes for Rails3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#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