Class: ActiveRecord::Extensions::ArrayExt

Inherits:
Object
  • Object
show all
Defined in:
lib/ar-extensions/extensions.rb

Overview

ActiveRecord::Extension to translate an Array of values into the approriate IN( … ) or NOT IN( … ) SQL.

Examples

Model.find :all, :conditions=>{ :id => [ 1,2,3 ] }

# the following three calls are equivalent
Model.find :all, :conditions=>{ :id_ne => [ 4,5,6 ] }
Model.find :all, :conditions=>{ :id_not => [ 4,5,6 ] }
Model.find :all, :conditions=>{ :id_not_in => [ 4,5,6 ] }

Constant Summary collapse

NOT_EQUAL_RGX =
/(.+)_(ne|not|not_in)/

Class Method Summary collapse

Class Method Details

.process(key, val, caller) ⇒ Object



179
180
181
182
183
184
185
186
187
188
# File 'lib/ar-extensions/extensions.rb', line 179

def self.process( key, val, caller )
  if val.is_a?( Array )
    match_data = key.to_s.match( NOT_EQUAL_RGX )
    key = match_data.captures[0] if match_data
    str = "#{caller.quoted_table_name}.#{caller.connection.quote_column_name( key )} " +
      (match_data ? 'NOT ' : '') + "IN( ? )"
    return Result.new( str, val )
  end
  nil
end