2
2
3
3
Active Record connects classes to relational database tables to establish an
4
4
almost zero-configuration persistence layer for applications. The library
5
- provides a base class that, when subclassed, sets up a mapping between the new
6
- class and an existing table in the database. In the context of an application,
7
- these classes are commonly referred to as *models*. Models can also be
8
- connected to other models; this is done by defining *associations*.
5
+ provides a base class, and subclassing an application specific version of
6
+ this base class sets up a mapping between the new class and an existing
7
+ table in the database. In the context of an application, these classes are
8
+ commonly referred to as *models*. Models can also be connected to other
9
+ models; this is done by defining *associations*.
9
10
10
11
Active Record relies heavily on naming in that it uses class and association
11
12
names to establish mappings between respective database tables and foreign key
@@ -17,7 +18,7 @@ A short rundown of some of the major features:
17
18
18
19
* Automated mapping between classes and tables, attributes and columns.
19
20
20
- class Product < ActiveRecord::Base
21
+ class Product < ApplicationRecord
21
22
end
22
23
23
24
The Product class is automatically mapped to the table named "products",
@@ -37,7 +38,7 @@ A short rundown of some of the major features:
37
38
38
39
* Associations between objects defined by simple class methods.
39
40
40
- class Firm < ActiveRecord::Base
41
+ class Firm < ApplicationRecord
41
42
has_many :clients
42
43
has_one :account
43
44
belongs_to :conglomerate
@@ -48,7 +49,7 @@ A short rundown of some of the major features:
48
49
49
50
* Aggregations of value objects.
50
51
51
- class Account < ActiveRecord::Base
52
+ class Account < ApplicationRecord
52
53
composed_of :balance, class_name: 'Money',
53
54
mapping: %w(balance amount)
54
55
composed_of :address,
@@ -60,7 +61,7 @@ A short rundown of some of the major features:
60
61
61
62
* Validation rules that can differ for new or existing objects.
62
63
63
- class Account < ActiveRecord::Base
64
+ class Account < ApplicationRecord
64
65
validates :subdomain, :name, :email_address, :password, presence: true
65
66
validates :subdomain, uniqueness: true
66
67
validates :terms_of_service, acceptance: true, on: :create
@@ -72,7 +73,7 @@ A short rundown of some of the major features:
72
73
73
74
* Callbacks available for the entire life cycle (instantiation, saving, destroying, validating, etc.).
74
75
75
- class Person < ActiveRecord::Base
76
+ class Person < ApplicationRecord
76
77
before_destroy :invalidate_payment_plan
77
78
# the `invalidate_payment_plan` method gets called just before Person#destroy
78
79
end
@@ -82,7 +83,7 @@ A short rundown of some of the major features:
82
83
83
84
* Inheritance hierarchies.
84
85
85
- class Company < ActiveRecord::Base ; end
86
+ class Company < ApplicationRecord ; end
86
87
class Firm < Company; end
87
88
class Client < Company; end
88
89
class PriorityClient < Client; end
0 commit comments