Skip to content

Commit 9f45fd9

Browse files
wangjohnrafaelfranca
authored andcommitted
Use ApplicationModel in the Rails guides.
The ApplicationModel should replace all instances where models subclass from ActiveRecord::Base.
1 parent cd919fa commit 9f45fd9

16 files changed

+246
-245
lines changed

guides/source/action_view_overview.md

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

10381038
```ruby
1039-
class Article < ActiveRecord::Base
1039+
class Article < ApplicationRecord
10401040
belongs_to :author
10411041
end
10421042

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

10741074
```ruby
1075-
class Article < ActiveRecord::Base
1075+
<<<<<<< HEAD
1076+
class Article < ApplicationRecord
10761077
belongs_to :author
10771078
end
10781079

1079-
class Author < ActiveRecord::Base
1080+
class Author < ApplicationRecord
10801081
has_many :articles
10811082
def name_with_initial
10821083
"#{first_name.first}. #{last_name}"
@@ -1108,11 +1109,11 @@ Returns `check_box` tags for the collection of existing return values of `method
11081109
Example object structure for use with this method:
11091110

11101111
```ruby
1111-
class Article < ActiveRecord::Base
1112+
class Article < ApplicationRecord
11121113
has_and_belongs_to_many :authors
11131114
end
11141115

1115-
class Author < ActiveRecord::Base
1116+
class Author < ApplicationRecord
11161117
has_and_belongs_to_many :articles
11171118
def name_with_initial
11181119
"#{first_name.first}. #{last_name}"
@@ -1153,12 +1154,12 @@ Returns a string of `option` tags, like `options_from_collection_for_select`, bu
11531154
Example object structure for use with this method:
11541155

11551156
```ruby
1156-
class Continent < ActiveRecord::Base
1157+
class Continent < ApplicationRecord
11571158
has_many :countries
11581159
# attribs: id, name
11591160
end
11601161

1161-
class Country < ActiveRecord::Base
1162+
class Country < ApplicationRecord
11621163
belongs_to :continent
11631164
# attribs: id, name, continent_id
11641165
end

guides/source/active_record_basics.md

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -132,10 +132,10 @@ Creating Active Record Models
132132
-----------------------------
133133

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

137137
```ruby
138-
class Product < ActiveRecord::Base
138+
class Product < ApplicationRecord
139139
end
140140
```
141141

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

174174
```ruby
175-
class Product < ActiveRecord::Base
175+
class Product < ApplicationRecord
176176
self.table_name = "PRODUCT"
177177
end
178178
```
@@ -193,7 +193,7 @@ It's also possible to override the column that should be used as the table's
193193
primary key using the `ActiveRecord::Base.primary_key=` method:
194194

195195
```ruby
196-
class Product < ActiveRecord::Base
196+
class Product < ApplicationRecord
197197
self.primary_key = "product_id"
198198
end
199199
```
@@ -320,7 +320,7 @@ they raise the exception `ActiveRecord::RecordInvalid` if validation fails.
320320
A quick example to illustrate:
321321

322322
```ruby
323-
class User < ActiveRecord::Base
323+
class User < ApplicationRecord
324324
validates :name, presence: true
325325
end
326326

guides/source/active_record_callbacks.md

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ Callbacks are methods that get called at certain moments of an object's life cyc
3131
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:
3232

3333
```ruby
34-
class User < ActiveRecord::Base
34+
class User < ApplicationRecord
3535
validates :login, :email, presence: true
3636

3737
before_validation :ensure_login_has_a_value
@@ -48,7 +48,7 @@ end
4848
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:
4949

5050
```ruby
51-
class User < ActiveRecord::Base
51+
class User < ApplicationRecord
5252
validates :login, :email, presence: true
5353

5454
before_create do
@@ -60,7 +60,7 @@ end
6060
Callbacks can also be registered to only fire on certain life cycle events:
6161

6262
```ruby
63-
class User < ActiveRecord::Base
63+
class User < ApplicationRecord
6464
before_validation :normalize_name, on: :create
6565

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

128128
```ruby
129-
class User < ActiveRecord::Base
129+
class User < ApplicationRecord
130130
after_initialize do |user|
131131
puts "You have initialized an object!"
132132
end
@@ -266,11 +266,11 @@ Relational Callbacks
266266
Callbacks work through model relationships, and can even be defined by them. Suppose an example where a user has many articles. A user's articles 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 `Article` model:
267267

268268
```ruby
269-
class User < ActiveRecord::Base
269+
class User < ApplicationRecord
270270
has_many :articles, dependent: :destroy
271271
end
272272

273-
class Article < ActiveRecord::Base
273+
class Article < ApplicationRecord
274274
after_destroy :log_destroy_action
275275

276276
def log_destroy_action
@@ -297,7 +297,7 @@ As with validations, we can also make the calling of a callback method condition
297297
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.
298298

299299
```ruby
300-
class Order < ActiveRecord::Base
300+
class Order < ApplicationRecord
301301
before_save :normalize_card_number, if: :paid_with_card?
302302
end
303303
```
@@ -307,7 +307,7 @@ end
307307
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:
308308

309309
```ruby
310-
class Order < ActiveRecord::Base
310+
class Order < ApplicationRecord
311311
before_save :normalize_card_number, if: "paid_with_card?"
312312
end
313313
```
@@ -317,7 +317,7 @@ end
317317
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:
318318

319319
```ruby
320-
class Order < ActiveRecord::Base
320+
class Order < ApplicationRecord
321321
before_save :normalize_card_number,
322322
if: Proc.new { |order| order.paid_with_card? }
323323
end
@@ -328,7 +328,7 @@ end
328328
When writing conditional callbacks, it is possible to mix both `:if` and `:unless` in the same callback declaration:
329329

330330
```ruby
331-
class Comment < ActiveRecord::Base
331+
class Comment < ApplicationRecord
332332
after_create :send_email_to_author, if: :author_wants_emails?,
333333
unless: Proc.new { |comment| comment.article.ignore_comments? }
334334
end
@@ -354,7 +354,7 @@ end
354354
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:
355355

356356
```ruby
357-
class PictureFile < ActiveRecord::Base
357+
class PictureFile < ApplicationRecord
358358
after_destroy PictureFileCallbacks.new
359359
end
360360
```
@@ -374,7 +374,7 @@ end
374374
If the callback method is declared this way, it won't be necessary to instantiate a `PictureFileCallbacks` object.
375375

376376
```ruby
377-
class PictureFile < ActiveRecord::Base
377+
class PictureFile < ApplicationRecord
378378
after_destroy PictureFileCallbacks
379379
end
380380
```
@@ -398,7 +398,7 @@ end
398398
By using the `after_commit` callback we can account for this case.
399399

400400
```ruby
401-
class PictureFile < ActiveRecord::Base
401+
class PictureFile < ApplicationRecord
402402
after_commit :delete_picture_file_from_disk, on: [:destroy]
403403

404404
def delete_picture_file_from_disk

guides/source/active_record_querying.md

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

2727
```ruby
28-
class Client < ActiveRecord::Base
28+
class Client < ApplicationRecord
2929
has_one :address
3030
has_many :orders
3131
has_and_belongs_to_many :roles
3232
end
3333
```
3434

3535
```ruby
36-
class Address < ActiveRecord::Base
36+
class Address < ApplicationRecord
3737
belongs_to :client
3838
end
3939
```
4040

4141
```ruby
42-
class Order < ActiveRecord::Base
42+
class Order < ApplicationRecord
4343
belongs_to :client, counter_cache: true
4444
end
4545
```
4646

4747
```ruby
48-
class Role < ActiveRecord::Base
48+
class Role < ApplicationRecord
4949
has_and_belongs_to_many :clients
5050
end
5151
```
@@ -715,7 +715,7 @@ SELECT "articles".* FROM "articles" WHERE (id > 10) ORDER BY id desc LIMIT 20
715715
The `reorder` method overrides the default scope order. For example:
716716

717717
```ruby
718-
class Article < ActiveRecord::Base
718+
class Article < ApplicationRecord
719719
has_many :comments, -> { order('posted_at DESC') }
720720
end
721721

@@ -864,7 +864,7 @@ This behavior can be turned off by setting `ActiveRecord::Base.lock_optimistical
864864
To override the name of the `lock_version` column, `ActiveRecord::Base` provides a class attribute called `locking_column`:
865865

866866
```ruby
867-
class Client < ActiveRecord::Base
867+
class Client < ApplicationRecord
868868
self.locking_column = :lock_client_column
869869
end
870870
```
@@ -940,26 +940,26 @@ Active Record lets you use the names of the [associations](association_basics.ht
940940
For example, consider the following `Category`, `Article`, `Comment`, `Guest` and `Tag` models:
941941

942942
```ruby
943-
class Category < ActiveRecord::Base
943+
class Category < ApplicationRecord
944944
has_many :articles
945945
end
946946

947-
class Article < ActiveRecord::Base
947+
class Article < ApplicationRecord
948948
belongs_to :category
949949
has_many :comments
950950
has_many :tags
951951
end
952952

953-
class Comment < ActiveRecord::Base
953+
class Comment < ApplicationRecord
954954
belongs_to :article
955955
has_one :guest
956956
end
957957

958-
class Guest < ActiveRecord::Base
958+
class Guest < ApplicationRecord
959959
belongs_to :comment
960960
end
961961

962-
class Tag < ActiveRecord::Base
962+
class Tag < ApplicationRecord
963963
belongs_to :article
964964
end
965965
```
@@ -1149,15 +1149,15 @@ Scoping allows you to specify commonly-used queries which can be referenced as m
11491149
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:
11501150

11511151
```ruby
1152-
class Article < ActiveRecord::Base
1152+
class Article < ApplicationRecord
11531153
scope :published, -> { where(published: true) }
11541154
end
11551155
```
11561156

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

11591159
```ruby
1160-
class Article < ActiveRecord::Base
1160+
class Article < ApplicationRecord
11611161
def self.published
11621162
where(published: true)
11631163
end
@@ -1167,7 +1167,7 @@ end
11671167
Scopes are also chainable within scopes:
11681168

11691169
```ruby
1170-
class Article < ActiveRecord::Base
1170+
class Article < ApplicationRecord
11711171
scope :published, -> { where(published: true) }
11721172
scope :published_and_commented, -> { published.where("comments_count > 0") }
11731173
end
@@ -1191,7 +1191,7 @@ category.articles.published # => [published articles belonging to this category]
11911191
Your scope can take arguments:
11921192

11931193
```ruby
1194-
class Article < ActiveRecord::Base
1194+
class Article < ApplicationRecord
11951195
scope :created_before, ->(time) { where("created_at < ?", time) }
11961196
end
11971197
```
@@ -1205,7 +1205,7 @@ Article.created_before(Time.zone.now)
12051205
However, this is just duplicating the functionality that would be provided to you by a class method.
12061206

12071207
```ruby
1208-
class Article < ActiveRecord::Base
1208+
class Article < ApplicationRecord
12091209
def self.created_before(time)
12101210
where("created_at < ?", time)
12111211
end
@@ -1252,7 +1252,7 @@ end
12521252
Just like `where` clauses scopes are merged using `AND` conditions.
12531253

12541254
```ruby
1255-
class User < ActiveRecord::Base
1255+
class User < ApplicationRecord
12561256
scope :active, -> { where state: 'active' }
12571257
scope :inactive, -> { where state: 'inactive' }
12581258
end
@@ -1281,7 +1281,7 @@ One important caveat is that `default_scope` will be prepended in
12811281
`scope` and `where` conditions.
12821282

12831283
```ruby
1284-
class User < ActiveRecord::Base
1284+
class User < ApplicationRecord
12851285
default_scope { where state: 'pending' }
12861286
scope :active, -> { where state: 'active' }
12871287
scope :inactive, -> { where state: 'inactive' }
@@ -1557,7 +1557,7 @@ a large or often-running query. However, any model method overrides will
15571557
not be available. For example:
15581558

15591559
```ruby
1560-
class Client < ActiveRecord::Base
1560+
class Client < ApplicationRecord
15611561
def name
15621562
"I am #{super}"
15631563
end
@@ -1592,7 +1592,7 @@ Person.ids
15921592
```
15931593

15941594
```ruby
1595-
class Person < ActiveRecord::Base
1595+
class Person < ApplicationRecord
15961596
self.primary_key = "person_id"
15971597
end
15981598

0 commit comments

Comments
 (0)