class method self.sanitize_sql_hash_for_conditions

Ruby on Rails 3.1.12

Since v2.2.3 Last seen in v3.1.12

Available in: v2.2.3 v2.3.18 v3.0.20 v3.1.12

Signature

self.sanitize_sql_hash_for_conditions(attrs, default_table_name = self.table_name)

Sanitizes a hash of attribute/value pairs into SQL conditions for a WHERE clause.

{ :name => "foo'bar", :group_id => 4 }
  # => "name='foo''bar' and group_id= 4"
{ :status => nil, :group_id => [1,2,3] }
  # => "status IS NULL and group_id IN (1,2,3)"
{ :age => 13..18 }
  # => "age BETWEEN 13 AND 18"
{ 'other_records.id' => 7 }
  # => "`other_records`.`id` = 7"
{ :other_records => { :id => 7 } }
  # => "`other_records`.`id` = 7"

And for value objects on a composed_of relationship:

{ :address => Address.new("123 abc st.", "chicago") }
  # => "address_street='123 abc st.' and address_city='chicago'"

Parameters

attrs req
default_table_name opt = self.table_name
Source
# File activerecord/lib/active_record/base.rb, line 1444
        def sanitize_sql_hash_for_conditions(attrs, default_table_name = self.table_name)
          attrs = expand_hash_conditions_for_aggregates(attrs)

          table = Arel::Table.new(table_name).alias(default_table_name)
          PredicateBuilder.build_from_hash(arel_engine, attrs, table).map { |b|
            connection.visitor.accept b
          }.join(' AND ')
        end

Defined in activerecord/lib/active_record/base.rb line 1444 · View on GitHub · Improve this page · Find usages on GitHub

Defined in ActiveRecord::Base

Type at least 2 characters to search.

↑↓ navigate · open · esc close