Permalink
Browse files

Added documentation about named bind variables

git-svn-id: http://svn-commit.rubyonrails.org/rails/trunk@1020 5ecf4fe2-1ee6-0310-87b1-e25e094e27de
  • Loading branch information...
1 parent dfadbfd commit 5cd38ca25d1742f2f1a4fa40145fe45c6415e17c @dhh dhh committed Mar 27, 2005
Showing with 10 additions and 0 deletions.
  1. +10 −0 activerecord/lib/active_record/base.rb
@@ -91,6 +91,15 @@ def initialize(errors)
# on the other hand, will sanitize the <tt>user_name</tt> and +password+ before inserting them in the query, which will ensure that
# an attacker can't escape the query and fake the login (or worse).
#
+ # When using multiple parameters in the conditions, it can easily become hard to read exactly what the fourth or fifth
+ # question mark is supposed to represent. In those cases, you can resort to named bind variables instead. That's done by replacing
+ # the question marks with symbols and supplying a hash with values for the matching symbol keys:
+ #
+ # Company.find_first([
+ # "id = :id AND name = :name AND division = :division AND created_at > :accounting_date",
+ # { :id => 3, :name => "37signals", :division => "First", :accounting_date => '2005-01-01' }
+ # ])
+ #
# == Overwriting default accessors
#
# All column values are automatically available through basic accessors on the Active Record object, but some times you
@@ -288,6 +297,7 @@ class << self # Class methods
# Person.find(1, :conditions => "associate_id = 5"
# Person.find(1, 2, 6, :conditions => "status = 'active'"
# Person.find([7, 17], :conditions => ["sanitize_me = ?", "bare'quote"]
+ # Person.find(25, :conditions => ["name = :name AND age = :age", { :name => "Mary", :age => 22 }]
#
# +RecordNotFound+ is raised if no record can be found.
def find(*args)

0 comments on commit 5cd38ca

Please sign in to comment.