From d3eacf9352ee34e2965c0b1781cdb8a1799686ec Mon Sep 17 00:00:00 2001 From: Neeraj Singh Date: Mon, 2 Aug 2010 13:07:18 -0400 Subject: Adding to AR::Base documentation about dynamically scopeded_by query User.scoped_by_user_name('David') --- activerecord/lib/active_record/base.rb | 26 ++++++++++++++-------- .../lib/active_record/dynamic_finder_match.rb | 4 ++-- 2 files changed, 19 insertions(+), 11 deletions(-) (limited to 'activerecord') diff --git a/activerecord/lib/active_record/base.rb b/activerecord/lib/active_record/base.rb index 5535079cd8..335f26d221 100644 --- a/activerecord/lib/active_record/base.rb +++ b/activerecord/lib/active_record/base.rb @@ -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 find_by_, find_last_by_, or find_all_by_, so you get finders + # to find_by_, find_last_by_, or find_all_by_ and thus produces finders # like Person.find_by_user_name, Person.find_all_by_last_name, and - # Payment.find_by_transaction_id. So instead of writing + # Payment.find_by_transaction_id. Instead of writing # Person.where(:user_name => user_name).first, you just do Person.find_by_user_name(user_name). # And instead of writing Person.where(:last_name => last_name).all, you just do # Person.find_all_by_last_name(last_name). # - # It's also possible to use multiple attributes in the same find by separating them with "_and_", - # so you get finders like Person.find_by_user_name_and_password or even - # Payment.find_by_purchaser_and_state_and_country. So instead of writing - # Person.where(:user_name => user_name, :password => password).first, you just do - # Person.find_by_user_name_and_password(user_name, password). + # 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 find_or_create_by_ 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 find_by_*, you can also use scoped_by_* to retrieve data. The good thing about + # using this feature is that the very first time result is returned using method_missing technique + # but after that the method is declared on the class. Henceforth method_missing 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 diff --git a/activerecord/lib/active_record/dynamic_finder_match.rb b/activerecord/lib/active_record/dynamic_finder_match.rb index dfb8a3ba60..0dc965bd26 100644 --- a/activerecord/lib/active_record/dynamic_finder_match.rb +++ b/activerecord/lib/active_record/dynamic_finder_match.rb @@ -2,8 +2,8 @@ module ActiveRecord # = Active Record Dynamic Finder Match # - # Provides dynamic attribute-based finders such as find_by_country - # if, for example, the Person 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) -- cgit v1.2.3