Skip to content

Commit

Permalink
Prefer find_by over dynamic finders in rdoc
Browse files Browse the repository at this point in the history
  • Loading branch information
rubys committed Apr 2, 2013
1 parent 6871b2e commit 04cda18
Show file tree
Hide file tree
Showing 10 changed files with 23 additions and 23 deletions.
2 changes: 1 addition & 1 deletion actionpack/lib/abstract_controller/helpers.rb
Expand Up @@ -29,7 +29,7 @@ def inherited(klass)
# helper_method :current_user, :logged_in? # helper_method :current_user, :logged_in?
# #
# def current_user # def current_user
# @current_user ||= User.find_by_id(session[:user]) # @current_user ||= User.find_by(id: session[:user])
# end # end
# #
# def logged_in? # def logged_in?
Expand Down
4 changes: 2 additions & 2 deletions actionpack/lib/action_controller/metal/http_authentication.rb
Expand Up @@ -29,7 +29,7 @@ module HttpAuthentication
# #
# protected # protected
# def set_account # def set_account
# @account = Account.find_by_url_name(request.subdomains.first) # @account = Account.find_by(url_name: request.subdomains.first)
# end # end
# #
# def authenticate # def authenticate
Expand Down Expand Up @@ -345,7 +345,7 @@ def opaque(secret_key)
# #
# protected # protected
# def set_account # def set_account
# @account = Account.find_by_url_name(request.subdomains.first) # @account = Account.find_by(url_name: request.subdomains.first)
# end # end
# #
# def authenticate # def authenticate
Expand Down
2 changes: 1 addition & 1 deletion actionpack/lib/action_controller/test_case.rb
Expand Up @@ -316,7 +316,7 @@ def load!
# assert_response :found # assert_response :found
# #
# # Assert that the controller really put the book in the database. # # Assert that the controller really put the book in the database.
# assert_not_nil Book.find_by_title("Love Hina") # assert_not_nil Book.find_by(title: "Love Hina")
# end # end
# end # end
# #
Expand Down
2 changes: 1 addition & 1 deletion activemodel/lib/active_model/dirty.rb
Expand Up @@ -46,7 +46,7 @@ module ActiveModel
# #
# A newly instantiated object is unchanged: # A newly instantiated object is unchanged:
# #
# person = Person.find_by_name('Uncle Bob') # person = Person.find_by(name: 'Uncle Bob')
# person.changed? # => false # person.changed? # => false
# #
# Change the name: # Change the name:
Expand Down
14 changes: 7 additions & 7 deletions activemodel/lib/active_model/secure_password.rb
Expand Up @@ -30,15 +30,15 @@ module ClassMethods
# end # end
# #
# user = User.new(name: 'david', password: '', password_confirmation: 'nomatch') # user = User.new(name: 'david', password: '', password_confirmation: 'nomatch')
# user.save # => false, password required # user.save # => false, password required
# user.password = 'mUc3m00RsqyRe' # user.password = 'mUc3m00RsqyRe'
# user.save # => false, confirmation doesn't match # user.save # => false, confirmation doesn't match
# user.password_confirmation = 'mUc3m00RsqyRe' # user.password_confirmation = 'mUc3m00RsqyRe'
# user.save # => true # user.save # => true
# user.authenticate('notright') # => false # user.authenticate('notright') # => false
# user.authenticate('mUc3m00RsqyRe') # => user # user.authenticate('mUc3m00RsqyRe') # => user
# User.find_by_name('david').try(:authenticate, 'notright') # => false # User.find_by(name: 'david').try(:authenticate, 'notright') # => false
# User.find_by_name('david').try(:authenticate, 'mUc3m00RsqyRe') # => user # User.find_by(name: 'david').try(:authenticate, 'mUc3m00RsqyRe') # => user
def has_secure_password(options = {}) def has_secure_password(options = {})
# Load bcrypt-ruby only when has_secure_password is used. # Load bcrypt-ruby only when has_secure_password is used.
# This is to avoid ActiveModel (and by extension the entire framework) # This is to avoid ActiveModel (and by extension the entire framework)
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/associations.rb
Expand Up @@ -988,7 +988,7 @@ def association_instance_set(name, association)
# associated objects themselves. So with +has_and_belongs_to_many+ and +has_many+ # associated objects themselves. So with +has_and_belongs_to_many+ and +has_many+
# <tt>:through</tt>, the join records will be deleted, but the associated records won't. # <tt>:through</tt>, the join records will be deleted, but the associated records won't.
# #
# This makes sense if you think about it: if you were to call <tt>post.tags.delete(Tag.find_by_name('food'))</tt> # This makes sense if you think about it: if you were to call <tt>post.tags.delete(Tag.find_by(name: 'food'))</tt>
# you would want the 'food' tag to be unlinked from the post, rather than for the tag itself # you would want the 'food' tag to be unlinked from the post, rather than for the tag itself
# to be removed from the database. # to be removed from the database.
# #
Expand Down
8 changes: 4 additions & 4 deletions activerecord/lib/active_record/autosave_association.rb
Expand Up @@ -62,14 +62,14 @@ module ActiveRecord
# Note that the model is _not_ yet removed from the database: # Note that the model is _not_ yet removed from the database:
# #
# id = post.author.id # id = post.author.id
# Author.find_by_id(id).nil? # => false # Author.find_by(id: id).nil? # => false
# #
# post.save # post.save
# post.reload.author # => nil # post.reload.author # => nil
# #
# Now it _is_ removed from the database: # Now it _is_ removed from the database:
# #
# Author.find_by_id(id).nil? # => true # Author.find_by(id: id).nil? # => true
# #
# === One-to-many Example # === One-to-many Example
# #
Expand Down Expand Up @@ -113,14 +113,14 @@ module ActiveRecord
# Note that the model is _not_ yet removed from the database: # Note that the model is _not_ yet removed from the database:
# #
# id = post.comments.last.id # id = post.comments.last.id
# Comment.find_by_id(id).nil? # => false # Comment.find_by(id: id).nil? # => false
# #
# post.save # post.save
# post.reload.comments.length # => 1 # post.reload.comments.length # => 1
# #
# Now it _is_ removed from the database: # Now it _is_ removed from the database:
# #
# Comment.find_by_id(id).nil? # => true # Comment.find_by(id: id).nil? # => true


module AutosaveAssociation module AutosaveAssociation
extend ActiveSupport::Concern extend ActiveSupport::Concern
Expand Down
6 changes: 3 additions & 3 deletions activerecord/lib/active_record/base.rb
Expand Up @@ -160,10 +160,10 @@ module ActiveRecord #:nodoc:
# #
# == Dynamic attribute-based finders # == Dynamic attribute-based finders
# #
# Dynamic attribute-based finders are a cleaner way of getting (and/or creating) objects # Dynamic attribute-based finders are a mildly deprecated way of getting (and/or creating) objects
# by simple queries without turning to SQL. They work by appending the name of an attribute # by simple queries without turning to SQL. They work by appending the name of an attribute
# to <tt>find_by_</tt> like <tt>Person.find_by_user_name</tt>. # to <tt>find_by_</tt> like <tt>Person.find_by_user_name</tt>.
# Instead of writing <tt>Person.where(user_name: user_name).first</tt>, you just do # Instead of writing <tt>Person.find_by(user_name: user_name)</tt>, you can use
# <tt>Person.find_by_user_name(user_name)</tt>. # <tt>Person.find_by_user_name(user_name)</tt>.
# #
# It's possible to add an exclamation point (!) on the end of the dynamic finders to get them to raise an # It's possible to add an exclamation point (!) on the end of the dynamic finders to get them to raise an
Expand All @@ -172,7 +172,7 @@ module ActiveRecord #:nodoc:
# #
# It's also possible to use multiple attributes in the same find by separating them with "_and_". # 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: user_name, password: password)
# Person.find_by_user_name_and_password(user_name, password) # with dynamic finder # Person.find_by_user_name_and_password(user_name, password) # with dynamic finder
# #
# It's even possible to call these dynamic finder methods on relations and named scopes. # It's even possible to call these dynamic finder methods on relations and named scopes.
Expand Down
4 changes: 2 additions & 2 deletions activerecord/lib/active_record/integration.rb
Expand Up @@ -21,7 +21,7 @@ module Integration
# <tt>resources :users</tt> route. Normally, +user_path+ will # <tt>resources :users</tt> route. Normally, +user_path+ will
# construct a path with the user object's 'id' in it: # construct a path with the user object's 'id' in it:
# #
# user = User.find_by_name('Phusion') # user = User.find_by(name: 'Phusion')
# user_path(user) # => "/users/1" # user_path(user) # => "/users/1"
# #
# You can override +to_param+ in your model to make +user_path+ construct # You can override +to_param+ in your model to make +user_path+ construct
Expand All @@ -33,7 +33,7 @@ module Integration
# end # end
# end # end
# #
# user = User.find_by_name('Phusion') # user = User.find_by(name: 'Phusion')
# user_path(user) # => "/users/Phusion" # user_path(user) # => "/users/Phusion"
def to_param def to_param
# We can't use alias_method here, because method 'id' optimizes itself on the fly. # We can't use alias_method here, because method 'id' optimizes itself on the fly.
Expand Down
2 changes: 1 addition & 1 deletion activerecord/lib/active_record/relation/calculations.rb
Expand Up @@ -82,7 +82,7 @@ def sum(*args)
# puts values["Drake"] # puts values["Drake"]
# # => 43 # # => 43
# #
# drake = Family.find_by_last_name('Drake') # drake = Family.find_by(last_name: 'Drake')
# values = Person.group(:family).maximum(:age) # Person belongs_to :family # values = Person.group(:family).maximum(:age) # Person belongs_to :family
# puts values[drake] # puts values[drake]
# # => 43 # # => 43
Expand Down

0 comments on commit 04cda18

Please sign in to comment.