Skip to content

Commit 47e3a21

Browse files
committed
Use ApplicationModel in the Rails guides.
The ApplicationModel should replace all instances where models subclass from ActiveRecord::Base.
1 parent 99ef8d2 commit 47e3a21

18 files changed

+254
-254
lines changed

guides/source/action_view_overview.md

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1035,11 +1035,11 @@ Returns `select` and `option` tags for the collection of existing return values
10351035
Example object structure for use with this method:
10361036

10371037
```ruby
1038-
class Post < ActiveRecord::Base
1038+
class Post < ApplicationRecord
10391039
belongs_to :author
10401040
end
10411041

1042-
class Author < ActiveRecord::Base
1042+
class Author < ApplicationRecord
10431043
has_many :posts
10441044
def name_with_initial
10451045
"#{first_name.first}. #{last_name}"
@@ -1071,11 +1071,11 @@ Returns `radio_button` tags for the collection of existing return values of `met
10711071
Example object structure for use with this method:
10721072

10731073
```ruby
1074-
class Post < ActiveRecord::Base
1074+
class Post < ApplicationRecord
10751075
belongs_to :author
10761076
end
10771077

1078-
class Author < ActiveRecord::Base
1078+
class Author < ApplicationRecord
10791079
has_many :posts
10801080
def name_with_initial
10811081
"#{first_name.first}. #{last_name}"
@@ -1107,11 +1107,11 @@ Returns `check_box` tags for the collection of existing return values of `method
11071107
Example object structure for use with this method:
11081108

11091109
```ruby
1110-
class Post < ActiveRecord::Base
1110+
class Post < ApplicationRecord
11111111
has_and_belongs_to_many :authors
11121112
end
11131113

1114-
class Author < ActiveRecord::Base
1114+
class Author < ApplicationRecord
11151115
has_and_belongs_to_many :posts
11161116
def name_with_initial
11171117
"#{first_name.first}. #{last_name}"
@@ -1152,12 +1152,12 @@ Returns a string of `option` tags, like `options_from_collection_for_select`, bu
11521152
Example object structure for use with this method:
11531153

11541154
```ruby
1155-
class Continent < ActiveRecord::Base
1155+
class Continent < ApplicationRecord
11561156
has_many :countries
11571157
# attribs: id, name
11581158
end
11591159

1160-
class Country < ActiveRecord::Base
1160+
class Country < ApplicationRecord
11611161
belongs_to :continent
11621162
# attribs: id, name, continent_id
11631163
end

guides/source/active_record_basics.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -130,10 +130,10 @@ Creating Active Record Models
130130
-----------------------------
131131

132132
It is very easy to create Active Record models. All you have to do is to
133-
subclass the `ActiveRecord::Base` class and you're good to go:
133+
subclass the `ApplicationRecord` class and you're good to go:
134134

135135
```ruby
136-
class Product < ActiveRecord::Base
136+
class Product < ApplicationRecord
137137
end
138138
```
139139

@@ -170,7 +170,7 @@ You can use the `ActiveRecord::Base.table_name=` method to specify the table
170170
name that should be used:
171171

172172
```ruby
173-
class Product < ActiveRecord::Base
173+
class Product < ApplicationRecord
174174
self.table_name = "PRODUCT"
175175
end
176176
```
@@ -191,7 +191,7 @@ It's also possible to override the column that should be used as the table's
191191
primary key using the `ActiveRecord::Base.primary_key=` method:
192192

193193
```ruby
194-
class Product < ActiveRecord::Base
194+
class Product < ApplicationRecord
195195
self.primary_key = "product_id"
196196
end
197197
```
@@ -318,7 +318,7 @@ they raise the exception `ActiveRecord::RecordInvalid` if validation fails.
318318
A quick example to illustrate:
319319

320320
```ruby
321-
class User < ActiveRecord::Base
321+
class User < ApplicationRecord
322322
validates :name, presence: true
323323
end
324324

guides/source/active_record_callbacks.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ Callbacks are methods that get called at certain moments of an object's life cyc
2929
In order to use the available callbacks, you need to register them. You can implement the callbacks as ordinary methods and use a macro-style class method to register them as callbacks:
3030

3131
```ruby
32-
class User < ActiveRecord::Base
32+
class User < ApplicationRecord
3333
validates :login, :email, presence: true
3434

3535
before_validation :ensure_login_has_a_value
@@ -46,7 +46,7 @@ end
4646
The macro-style class methods can also receive a block. Consider using this style if the code inside your block is so short that it fits in a single line:
4747

4848
```ruby
49-
class User < ActiveRecord::Base
49+
class User < ApplicationRecord
5050
validates :login, :email, presence: true
5151

5252
before_create do
@@ -58,7 +58,7 @@ end
5858
Callbacks can also be registered to only fire on certain lifecycle events:
5959

6060
```ruby
61-
class User < ActiveRecord::Base
61+
class User < ApplicationRecord
6262
before_validation :normalize_name, on: :create
6363

6464
# :on takes an array as well
@@ -121,7 +121,7 @@ The `after_find` callback will be called whenever Active Record loads a record f
121121
The `after_initialize` and `after_find` callbacks have no `before_*` counterparts, but they can be registered just like the other Active Record callbacks.
122122

123123
```ruby
124-
class User < ActiveRecord::Base
124+
class User < ApplicationRecord
125125
after_initialize do |user|
126126
puts "You have initialized an object!"
127127
end
@@ -212,11 +212,11 @@ Relational Callbacks
212212
Callbacks work through model relationships, and can even be defined by them. Suppose an example where a user has many posts. A user's posts should be destroyed if the user is destroyed. Let's add an `after_destroy` callback to the `User` model by way of its relationship to the `Post` model:
213213

214214
```ruby
215-
class User < ActiveRecord::Base
215+
class User < ApplicationRecord
216216
has_many :posts, dependent: :destroy
217217
end
218218

219-
class Post < ActiveRecord::Base
219+
class Post < ApplicationRecord
220220
after_destroy :log_destroy_action
221221

222222
def log_destroy_action
@@ -243,7 +243,7 @@ As with validations, we can also make the calling of a callback method condition
243243
You can associate the `:if` and `:unless` options with a symbol corresponding to the name of a predicate method that will get called right before the callback. When using the `:if` option, the callback won't be executed if the predicate method returns false; when using the `:unless` option, the callback won't be executed if the predicate method returns true. This is the most common option. Using this form of registration it is also possible to register several different predicates that should be called to check if the callback should be executed.
244244

245245
```ruby
246-
class Order < ActiveRecord::Base
246+
class Order < ApplicationRecord
247247
before_save :normalize_card_number, if: :paid_with_card?
248248
end
249249
```
@@ -253,7 +253,7 @@ end
253253
You can also use a string that will be evaluated using `eval` and hence needs to contain valid Ruby code. You should use this option only when the string represents a really short condition:
254254

255255
```ruby
256-
class Order < ActiveRecord::Base
256+
class Order < ApplicationRecord
257257
before_save :normalize_card_number, if: "paid_with_card?"
258258
end
259259
```
@@ -263,7 +263,7 @@ end
263263
Finally, it is possible to associate `:if` and `:unless` with a `Proc` object. This option is best suited when writing short validation methods, usually one-liners:
264264

265265
```ruby
266-
class Order < ActiveRecord::Base
266+
class Order < ApplicationRecord
267267
before_save :normalize_card_number,
268268
if: Proc.new { |order| order.paid_with_card? }
269269
end
@@ -274,7 +274,7 @@ end
274274
When writing conditional callbacks, it is possible to mix both `:if` and `:unless` in the same callback declaration:
275275

276276
```ruby
277-
class Comment < ActiveRecord::Base
277+
class Comment < ApplicationRecord
278278
after_create :send_email_to_author, if: :author_wants_emails?,
279279
unless: Proc.new { |comment| comment.post.ignore_comments? }
280280
end
@@ -300,7 +300,7 @@ end
300300
When declared inside a class, as above, the callback methods will receive the model object as a parameter. We can now use the callback class in the model:
301301

302302
```ruby
303-
class PictureFile < ActiveRecord::Base
303+
class PictureFile < ApplicationRecord
304304
after_destroy PictureFileCallbacks.new
305305
end
306306
```
@@ -320,7 +320,7 @@ end
320320
If the callback method is declared this way, it won't be necessary to instantiate a `PictureFileCallbacks` object.
321321

322322
```ruby
323-
class PictureFile < ActiveRecord::Base
323+
class PictureFile < ApplicationRecord
324324
after_destroy PictureFileCallbacks
325325
end
326326
```
@@ -344,7 +344,7 @@ end
344344
By using the `after_commit` callback we can account for this case.
345345

346346
```ruby
347-
class PictureFile < ActiveRecord::Base
347+
class PictureFile < ApplicationRecord
348348
after_commit :delete_picture_file_from_disk, on: [:destroy]
349349

350350
def delete_picture_file_from_disk

guides/source/active_record_querying.md

Lines changed: 22 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,27 +22,27 @@ Code examples throughout this guide will refer to one or more of the following m
2222
TIP: All of the following models use `id` as the primary key, unless specified otherwise.
2323

2424
```ruby
25-
class Client < ActiveRecord::Base
25+
class Client < ApplicationRecord
2626
has_one :address
2727
has_many :orders
2828
has_and_belongs_to_many :roles
2929
end
3030
```
3131

3232
```ruby
33-
class Address < ActiveRecord::Base
33+
class Address < ApplicationRecord
3434
belongs_to :client
3535
end
3636
```
3737

3838
```ruby
39-
class Order < ActiveRecord::Base
39+
class Order < ApplicationRecord
4040
belongs_to :client, counter_cache: true
4141
end
4242
```
4343

4444
```ruby
45-
class Role < ActiveRecord::Base
45+
class Role < ApplicationRecord
4646
has_and_belongs_to_many :clients
4747
end
4848
```
@@ -747,7 +747,7 @@ SELECT "posts".* FROM "posts" WHERE (id > 10) ORDER BY id desc LIMIT 20
747747
The `reorder` method overrides the default scope order. For example:
748748

749749
```ruby
750-
class Post < ActiveRecord::Base
750+
class Post < ApplicationRecord
751751
..
752752
..
753753
has_many :comments, -> { order('posted_at DESC') }
@@ -870,7 +870,7 @@ This behavior can be turned off by setting `ActiveRecord::Base.lock_optimistical
870870
To override the name of the `lock_version` column, `ActiveRecord::Base` provides a class attribute called `locking_column`:
871871

872872
```ruby
873-
class Client < ActiveRecord::Base
873+
class Client < ApplicationRecord
874874
self.locking_column = :lock_client_column
875875
end
876876
```
@@ -946,26 +946,26 @@ Active Record lets you use the names of the [associations](association_basics.ht
946946
For example, consider the following `Category`, `Post`, `Comments` and `Guest` models:
947947

948948
```ruby
949-
class Category < ActiveRecord::Base
949+
class Category < ApplicationRecord
950950
has_many :posts
951951
end
952952

953-
class Post < ActiveRecord::Base
953+
class Post < ApplicationRecord
954954
belongs_to :category
955955
has_many :comments
956956
has_many :tags
957957
end
958958

959-
class Comment < ActiveRecord::Base
959+
class Comment < ApplicationRecord
960960
belongs_to :post
961961
has_one :guest
962962
end
963963

964-
class Guest < ActiveRecord::Base
964+
class Guest < ApplicationRecord
965965
belongs_to :comment
966966
end
967967

968-
class Tag < ActiveRecord::Base
968+
class Tag < ApplicationRecord
969969
belongs_to :post
970970
end
971971
```
@@ -1142,15 +1142,15 @@ Scoping allows you to specify commonly-used queries which can be referenced as m
11421142
To define a simple scope, we use the `scope` method inside the class, passing the query that we'd like to run when this scope is called:
11431143

11441144
```ruby
1145-
class Post < ActiveRecord::Base
1145+
class Post < ApplicationRecord
11461146
scope :published, -> { where(published: true) }
11471147
end
11481148
```
11491149

11501150
This is exactly the same as defining a class method, and which you use is a matter of personal preference:
11511151

11521152
```ruby
1153-
class Post < ActiveRecord::Base
1153+
class Post < ApplicationRecord
11541154
def self.published
11551155
where(published: true)
11561156
end
@@ -1160,7 +1160,7 @@ end
11601160
Scopes are also chainable within scopes:
11611161

11621162
```ruby
1163-
class Post < ActiveRecord::Base
1163+
class Post < ApplicationRecord
11641164
scope :published, -> { where(published: true) }
11651165
scope :published_and_commented, -> { published.where("comments_count > 0") }
11661166
end
@@ -1184,7 +1184,7 @@ category.posts.published # => [published posts belonging to this category]
11841184
Your scope can take arguments:
11851185

11861186
```ruby
1187-
class Post < ActiveRecord::Base
1187+
class Post < ApplicationRecord
11881188
scope :created_before, ->(time) { where("created_at < ?", time) }
11891189
end
11901190
```
@@ -1198,7 +1198,7 @@ Post.created_before(Time.zone.now)
11981198
However, this is just duplicating the functionality that would be provided to you by a class method.
11991199

12001200
```ruby
1201-
class Post < ActiveRecord::Base
1201+
class Post < ApplicationRecord
12021202
def self.created_before(time)
12031203
where("created_at < ?", time)
12041204
end
@@ -1216,7 +1216,7 @@ category.posts.created_before(time)
12161216
Just like `where` clauses scopes are merged using `AND` conditions.
12171217

12181218
```ruby
1219-
class User < ActiveRecord::Base
1219+
class User < ApplicationRecord
12201220
scope :active, -> { where state: 'active' }
12211221
scope :inactive, -> { where state: 'inactive' }
12221222
end
@@ -1245,7 +1245,7 @@ One important caveat is that `default_scope` will be overridden by
12451245
`scope` and `where` conditions.
12461246

12471247
```ruby
1248-
class User < ActiveRecord::Base
1248+
class User < ApplicationRecord
12491249
default_scope { where state: 'pending' }
12501250
scope :active, -> { where state: 'active' }
12511251
scope :inactive, -> { where state: 'inactive' }
@@ -1271,7 +1271,7 @@ If we wish for a scope to be applied across all queries to the model we can use
12711271
`default_scope` method within the model itself.
12721272

12731273
```ruby
1274-
class Client < ActiveRecord::Base
1274+
class Client < ApplicationRecord
12751275
default_scope { where("removed_at IS NULL") }
12761276
end
12771277
```
@@ -1287,7 +1287,7 @@ If you need to do more complex things with a default scope, you can alternativel
12871287
define it as a class method:
12881288

12891289
```ruby
1290-
class Client < ActiveRecord::Base
1290+
class Client < ApplicationRecord
12911291
def self.default_scope
12921292
# Should return an ActiveRecord::Relation.
12931293
end
@@ -1490,7 +1490,7 @@ a large or often-running query. However, any model method overrides will
14901490
not be available. For example:
14911491

14921492
```ruby
1493-
class Client < ActiveRecord::Base
1493+
class Client < ApplicationRecord
14941494
def name
14951495
"I am #{super}"
14961496
end
@@ -1525,7 +1525,7 @@ Person.ids
15251525
```
15261526

15271527
```ruby
1528-
class Person < ActiveRecord::Base
1528+
class Person < ApplicationRecord
15291529
self.primary_key = "person_id"
15301530
end
15311531

0 commit comments

Comments
 (0)