Skip to content
This repository
Browse code

Merge pull request #10054 from rubys/use_find_by_in_rdoc

Prefer find_by over dynamic finders in rdoc
  • Loading branch information...
commit 4ae089b661a78180bb978a34d294e8a9e076f652 2 parents defb5a2 + 04cda18
Santiago Pastorino authored
2  actionpack/lib/abstract_controller/helpers.rb
@@ -29,7 +29,7 @@ def inherited(klass)
29 29
       #     helper_method :current_user, :logged_in?
30 30
       #
31 31
       #     def current_user
32  
-      #       @current_user ||= User.find_by_id(session[:user])
  32
+      #       @current_user ||= User.find_by(id: session[:user])
33 33
       #     end
34 34
       #
35 35
       #     def logged_in?
4  actionpack/lib/action_controller/metal/http_authentication.rb
@@ -29,7 +29,7 @@ module HttpAuthentication
29 29
     #
30 30
     #     protected
31 31
     #       def set_account
32  
-    #         @account = Account.find_by_url_name(request.subdomains.first)
  32
+    #         @account = Account.find_by(url_name: request.subdomains.first)
33 33
     #       end
34 34
     #
35 35
     #       def authenticate
@@ -345,7 +345,7 @@ def opaque(secret_key)
345 345
     #
346 346
     #     protected
347 347
     #       def set_account
348  
-    #         @account = Account.find_by_url_name(request.subdomains.first)
  348
+    #         @account = Account.find_by(url_name: request.subdomains.first)
349 349
     #       end
350 350
     #
351 351
     #       def authenticate
2  actionpack/lib/action_controller/test_case.rb
@@ -316,7 +316,7 @@ def load!
316 316
   #       assert_response :found
317 317
   #
318 318
   #       # Assert that the controller really put the book in the database.
319  
-  #       assert_not_nil Book.find_by_title("Love Hina")
  319
+  #       assert_not_nil Book.find_by(title: "Love Hina")
320 320
   #     end
321 321
   #   end
322 322
   #
2  activemodel/lib/active_model/dirty.rb
@@ -46,7 +46,7 @@ module ActiveModel
46 46
   #
47 47
   # A newly instantiated object is unchanged:
48 48
   #
49  
-  #   person = Person.find_by_name('Uncle Bob')
  49
+  #   person = Person.find_by(name: 'Uncle Bob')
50 50
   #   person.changed?       # => false
51 51
   #
52 52
   # Change the name:
14  activemodel/lib/active_model/secure_password.rb
@@ -30,15 +30,15 @@ module ClassMethods
30 30
       #   end
31 31
       #
32 32
       #   user = User.new(name: 'david', password: '', password_confirmation: 'nomatch')
33  
-      #   user.save                                                      # => false, password required
  33
+      #   user.save                                                       # => false, password required
34 34
       #   user.password = 'mUc3m00RsqyRe'
35  
-      #   user.save                                                      # => false, confirmation doesn't match
  35
+      #   user.save                                                       # => false, confirmation doesn't match
36 36
       #   user.password_confirmation = 'mUc3m00RsqyRe'
37  
-      #   user.save                                                      # => true
38  
-      #   user.authenticate('notright')                                  # => false
39  
-      #   user.authenticate('mUc3m00RsqyRe')                             # => user
40  
-      #   User.find_by_name('david').try(:authenticate, 'notright')      # => false
41  
-      #   User.find_by_name('david').try(:authenticate, 'mUc3m00RsqyRe') # => user
  37
+      #   user.save                                                       # => true
  38
+      #   user.authenticate('notright')                                   # => false
  39
+      #   user.authenticate('mUc3m00RsqyRe')                              # => user
  40
+      #   User.find_by(name: 'david').try(:authenticate, 'notright')      # => false
  41
+      #   User.find_by(name: 'david').try(:authenticate, 'mUc3m00RsqyRe') # => user
42 42
       def has_secure_password(options = {})
43 43
         # Load bcrypt-ruby only when has_secure_password is used.
44 44
         # This is to avoid ActiveModel (and by extension the entire framework)
2  activerecord/lib/active_record/associations.rb
@@ -988,7 +988,7 @@ def association_instance_set(name, association)
988 988
     # associated objects themselves. So with +has_and_belongs_to_many+ and +has_many+
989 989
     # <tt>:through</tt>, the join records will be deleted, but the associated records won't.
990 990
     #
991  
-    # This makes sense if you think about it: if you were to call <tt>post.tags.delete(Tag.find_by_name('food'))</tt>
  991
+    # This makes sense if you think about it: if you were to call <tt>post.tags.delete(Tag.find_by(name: 'food'))</tt>
992 992
     # you would want the 'food' tag to be unlinked from the post, rather than for the tag itself
993 993
     # to be removed from the database.
994 994
     #
8  activerecord/lib/active_record/autosave_association.rb
@@ -62,14 +62,14 @@ module ActiveRecord
62 62
   # Note that the model is _not_ yet removed from the database:
63 63
   #
64 64
   #   id = post.author.id
65  
-  #   Author.find_by_id(id).nil? # => false
  65
+  #   Author.find_by(id: id).nil? # => false
66 66
   #
67 67
   #   post.save
68 68
   #   post.reload.author # => nil
69 69
   #
70 70
   # Now it _is_ removed from the database:
71 71
   #
72  
-  #   Author.find_by_id(id).nil? # => true
  72
+  #   Author.find_by(id: id).nil? # => true
73 73
   #
74 74
   # === One-to-many Example
75 75
   #
@@ -113,14 +113,14 @@ module ActiveRecord
113 113
   # Note that the model is _not_ yet removed from the database:
114 114
   #
115 115
   #   id = post.comments.last.id
116  
-  #   Comment.find_by_id(id).nil? # => false
  116
+  #   Comment.find_by(id: id).nil? # => false
117 117
   #
118 118
   #   post.save
119 119
   #   post.reload.comments.length # => 1
120 120
   #
121 121
   # Now it _is_ removed from the database:
122 122
   #
123  
-  #   Comment.find_by_id(id).nil? # => true
  123
+  #   Comment.find_by(id: id).nil? # => true
124 124
 
125 125
   module AutosaveAssociation
126 126
     extend ActiveSupport::Concern
6  activerecord/lib/active_record/base.rb
@@ -160,10 +160,10 @@ module ActiveRecord #:nodoc:
160 160
   #
161 161
   # == Dynamic attribute-based finders
162 162
   #
163  
-  # Dynamic attribute-based finders are a cleaner way of getting (and/or creating) objects
  163
+  # Dynamic attribute-based finders are a mildly deprecated way of getting (and/or creating) objects
164 164
   # by simple queries without turning to SQL. They work by appending the name of an attribute
165 165
   # to <tt>find_by_</tt> like <tt>Person.find_by_user_name</tt>.
166  
-  # Instead of writing <tt>Person.where(user_name: user_name).first</tt>, you just do
  166
+  # Instead of writing <tt>Person.find_by(user_name: user_name)</tt>, you can use
167 167
   # <tt>Person.find_by_user_name(user_name)</tt>.
168 168
   #
169 169
   # It's possible to add an exclamation point (!) on the end of the dynamic finders to get them to raise an
@@ -172,7 +172,7 @@ module ActiveRecord #:nodoc:
172 172
   #
173 173
   # It's also possible to use multiple attributes in the same find by separating them with "_and_".
174 174
   #
175  
-  #  Person.where(user_name: user_name, password: password).first
  175
+  #  Person.find_by(user_name: user_name, password: password)
176 176
   #  Person.find_by_user_name_and_password(user_name, password) # with dynamic finder
177 177
   #
178 178
   # It's even possible to call these dynamic finder methods on relations and named scopes.
4  activerecord/lib/active_record/integration.rb
@@ -21,7 +21,7 @@ module Integration
21 21
     # <tt>resources :users</tt> route. Normally, +user_path+ will
22 22
     # construct a path with the user object's 'id' in it:
23 23
     #
24  
-    #   user = User.find_by_name('Phusion')
  24
+    #   user = User.find_by(name: 'Phusion')
25 25
     #   user_path(user)  # => "/users/1"
26 26
     #
27 27
     # You can override +to_param+ in your model to make +user_path+ construct
@@ -33,7 +33,7 @@ module Integration
33 33
     #     end
34 34
     #   end
35 35
     #
36  
-    #   user = User.find_by_name('Phusion')
  36
+    #   user = User.find_by(name: 'Phusion')
37 37
     #   user_path(user)  # => "/users/Phusion"
38 38
     def to_param
39 39
       # We can't use alias_method here, because method 'id' optimizes itself on the fly.
2  activerecord/lib/active_record/relation/calculations.rb
@@ -82,7 +82,7 @@ def sum(*args)
82 82
     #       puts values["Drake"]
83 83
     #       # => 43
84 84
     #
85  
-    #       drake  = Family.find_by_last_name('Drake')
  85
+    #       drake  = Family.find_by(last_name: 'Drake')
86 86
     #       values = Person.group(:family).maximum(:age) # Person belongs_to :family
87 87
     #       puts values[drake]
88 88
     #       # => 43

0 notes on commit 4ae089b

Please sign in to comment.
Something went wrong with that request. Please try again.