1
0
Fork 0
mirror of https://github.com/rails/rails.git synced 2022-11-09 12:12:34 -05:00

Adding to AR::Base documentation about dynamically scopeded_by query

User.scoped_by_user_name('David')
This commit is contained in:
Neeraj Singh 2010-08-02 13:07:18 -04:00
parent 1ce40ca562
commit d3eacf9352
2 changed files with 19 additions and 11 deletions

View file

@ -166,20 +166,22 @@ module ActiveRecord #:nodoc:
#
# Dynamic attribute-based finders are a cleaner way of getting (and/or creating) objects
# by simple queries without turning to SQL. They work by appending the name of an attribute
# to <tt>find_by_</tt>, <tt>find_last_by_</tt>, or <tt>find_all_by_</tt>, so you get finders
# to <tt>find_by_</tt>, <tt>find_last_by_</tt>, or <tt>find_all_by_</tt> and thus produces finders
# like <tt>Person.find_by_user_name</tt>, <tt>Person.find_all_by_last_name</tt>, and
# <tt>Payment.find_by_transaction_id</tt>. So instead of writing
# <tt>Payment.find_by_transaction_id</tt>. Instead of writing
# <tt>Person.where(:user_name => user_name).first</tt>, you just do <tt>Person.find_by_user_name(user_name)</tt>.
# And instead of writing <tt>Person.where(:last_name => last_name).all</tt>, you just do
# <tt>Person.find_all_by_last_name(last_name)</tt>.
#
# It's also possible to use multiple attributes in the same find by separating them with "_and_",
# so you get finders like <tt>Person.find_by_user_name_and_password</tt> or even
# <tt>Payment.find_by_purchaser_and_state_and_country</tt>. So instead of writing
# <tt>Person.where(:user_name => user_name, :password => password).first</tt>, you just do
# <tt>Person.find_by_user_name_and_password(user_name, password)</tt>.
# It's also possible to use multiple attributes in the same find by separating them with "_and_".
#
# Person.where(:user_name => user_name, :password => password).first
# Person.find_by_user_name_and_password #with dynamic finder
#
# Person.where(:user_name => user_name, :password => password, :gender => 'male').first
# Payment.find_by_user_name_and_password_and_gender
#
# It's even possible to call these dynamic finder methods on relations and named scopes. For example :
# It's even possible to call these dynamic finder methods on relations and named scopes.
#
# Payment.order("created_on").find_all_by_amount(50)
# Payment.pending.find_last_by_amount(100)
@ -187,7 +189,7 @@ module ActiveRecord #:nodoc:
# The same dynamic finder style can be used to create the object if it doesn't already exist.
# This dynamic finder is called with <tt>find_or_create_by_</tt> and will return the object if
# it already exists and otherwise creates it, then returns it. Protected attributes won't be set
# unless they are given in a block. For example:
# unless they are given in a block.
#
# # No 'Summer' tag exists
# Tag.find_or_create_by_name("Summer") # equal to Tag.create(:name => "Summer")
@ -213,6 +215,12 @@ module ActiveRecord #:nodoc:
# That will either find an existing tag named "rails", or create a new one while setting the
# user that created it.
#
# Just like <tt>find_by_*</tt>, you can also use <tt>scoped_by_*</tt> to retrieve data. The good thing about
# using this feature is that the very first time result is returned using <tt>method_missing</tt> technique
# but after that the method is declared on the class. Henceforth <tt>method_missing</tt> will not be hit.
#
# User.scoped_by_user_name('David')
#
# == Saving arrays, hashes, and other non-mappable objects in text columns
#
# Active Record can serialize any object in text columns using YAML. To do so, you must

View file

@ -2,8 +2,8 @@ module ActiveRecord
# = Active Record Dynamic Finder Match
#
# Provides dynamic attribute-based finders such as <tt>find_by_country</tt>
# if, for example, the <tt>Person</tt> has an attribute with that name.
# Refer to ActiveRecord::Base documentation for Dynamic attribute-based finders for detailed info
#
class DynamicFinderMatch
def self.match(method)
df_match = self.new(method)