aboutsummaryrefslogtreecommitdiffstats
path: root/guides/source/active_record_basics.md
diff options
context:
space:
mode:
authorGenadi Samokovarov <gsamokovarov@gmail.com>2015-12-12 14:25:00 +0100
committerGenadi Samokovarov <gsamokovarov@gmail.com>2015-12-16 10:30:09 +0100
commit2067fff9e38df4e37bdbfc021cd6bb0c2d393a2a (patch)
tree18c5067747cc422aed35dce0417f14ddf36346a7 /guides/source/active_record_basics.md
parente73fe1dd8c2740ae29e7a7f48d71a62b46e6b49d (diff)
downloadrails-2067fff9e38df4e37bdbfc021cd6bb0c2d393a2a.tar.gz
rails-2067fff9e38df4e37bdbfc021cd6bb0c2d393a2a.tar.bz2
rails-2067fff9e38df4e37bdbfc021cd6bb0c2d393a2a.zip
Introduce ApplicationRecord, an Active Record layer supertype
It's pretty common for folks to monkey patch `ActiveRecord::Base` to work around an issue or introduce extra functionality. Instead of shoving even more stuff in `ActiveRecord::Base`, `ApplicationRecord` can hold all those custom work the apps may need. Now, we don't wanna encourage all of the application models to inherit from `ActiveRecord::Base`, but we can encourage all the models that do, to inherit from `ApplicationRecord`. Newly generated applications have `app/models/application_record.rb` present by default. The model generators are smart enough to recognize that newly generated models have to inherit from `ApplicationRecord`, but only if it's present.
Diffstat (limited to 'guides/source/active_record_basics.md')
-rw-r--r--guides/source/active_record_basics.md15
1 files changed, 8 insertions, 7 deletions
diff --git a/guides/source/active_record_basics.md b/guides/source/active_record_basics.md
index abb22f9cb8..8bd135ddd5 100644
--- a/guides/source/active_record_basics.md
+++ b/guides/source/active_record_basics.md
@@ -132,10 +132,10 @@ Creating Active Record Models
-----------------------------
It is very easy to create Active Record models. All you have to do is to
-subclass the `ActiveRecord::Base` class and you're good to go:
+subclass the `ApplicationRecord` class and you're good to go:
```ruby
-class Product < ActiveRecord::Base
+class Product < ApplicationRecord
end
```
@@ -168,11 +168,12 @@ What if you need to follow a different naming convention or need to use your
Rails application with a legacy database? No problem, you can easily override
the default conventions.
-You can use the `ActiveRecord::Base.table_name=` method to specify the table
-name that should be used:
+`ApplicationRecord` inherits from `ActionController::Base`, which defines a
+number of helpful methods. You can use the `ActiveRecord::Base.table_name=`
+method to specify the table name that should be used:
```ruby
-class Product < ActiveRecord::Base
+class Product < ApplicationRecord
self.table_name = "my_products"
end
```
@@ -193,7 +194,7 @@ It's also possible to override the column that should be used as the table's
primary key using the `ActiveRecord::Base.primary_key=` method:
```ruby
-class Product < ActiveRecord::Base
+class Product < ApplicationRecord
self.primary_key = "product_id"
end
```
@@ -320,7 +321,7 @@ they raise the exception `ActiveRecord::RecordInvalid` if validation fails.
A quick example to illustrate:
```ruby
-class User < ActiveRecord::Base
+class User < ApplicationRecord
validates :name, presence: true
end