@@ -179,11 +179,11 @@ Use nested routes to express better the relationship between Active Record model
179
179
180
180
[source,ruby]
181
181
----
182
- class Post < ActiveRecord::Base
182
+ class Post < ApplicationRecord
183
183
has_many :comments
184
184
end
185
185
186
- class Comment < ActiveRecord::Base
186
+ class Comment < ApplicationRecord
187
187
belongs_to :post
188
188
end
189
189
@@ -402,7 +402,7 @@ Avoid altering Active Record defaults (table names, primary key, etc) unless you
402
402
[source,ruby]
403
403
----
404
404
# bad - don't do this if you can modify the schema
405
- class Transaction < ActiveRecord::Base
405
+ class Transaction < ApplicationRecord
406
406
self.table_name = 'order'
407
407
...
408
408
end
@@ -416,7 +416,7 @@ lead to broken code.
416
416
417
417
[source,ruby]
418
418
----
419
- class Transaction < ActiveRecord::Base
419
+ class Transaction < ApplicationRecord
420
420
# bad - implicit values - ordering matters
421
421
enum type: %i[credit debit]
422
422
@@ -434,7 +434,7 @@ Group macro-style methods (`has_many`, `validates`, etc) in the beginning of the
434
434
435
435
[source,ruby]
436
436
----
437
- class User < ActiveRecord::Base
437
+ class User < ApplicationRecord
438
438
# keep the default scope first (if any)
439
439
default_scope { where(active: true) }
440
440
@@ -479,26 +479,26 @@ Using `has_many :through` allows additional attributes and validations on the jo
479
479
[source,ruby]
480
480
----
481
481
# not so good - using has_and_belongs_to_many
482
- class User < ActiveRecord::Base
482
+ class User < ApplicationRecord
483
483
has_and_belongs_to_many :groups
484
484
end
485
485
486
- class Group < ActiveRecord::Base
486
+ class Group < ApplicationRecord
487
487
has_and_belongs_to_many :users
488
488
end
489
489
490
490
# preferred way - using has_many :through
491
- class User < ActiveRecord::Base
491
+ class User < ApplicationRecord
492
492
has_many :memberships
493
493
has_many :groups, through: :memberships
494
494
end
495
495
496
- class Membership < ActiveRecord::Base
496
+ class Membership < ApplicationRecord
497
497
belongs_to :user
498
498
belongs_to :group
499
499
end
500
500
501
- class Group < ActiveRecord::Base
501
+ class Group < ApplicationRecord
502
502
has_many :memberships
503
503
has_many :users, through: :memberships
504
504
end
@@ -630,7 +630,7 @@ Use named scopes freely.
630
630
631
631
[source,ruby]
632
632
----
633
- class User < ActiveRecord::Base
633
+ class User < ApplicationRecord
634
634
scope :active, -> { where(active: true) }
635
635
scope :inactive, -> { where(active: false) }
636
636
@@ -645,7 +645,7 @@ Arguably you can define even simpler scopes like this.
645
645
646
646
[source,ruby]
647
647
----
648
- class User < ActiveRecord::Base
648
+ class User < ApplicationRecord
649
649
def self.with_orders
650
650
joins(:orders).select('distinct(users.id)')
651
651
end
@@ -792,12 +792,12 @@ Define the `dependent` option to the `has_many` and `has_one` associations.
792
792
[source,ruby]
793
793
----
794
794
# bad
795
- class Post < ActiveRecord::Base
795
+ class Post < ApplicationRecord
796
796
has_many :comments
797
797
end
798
798
799
799
# good
800
- class Post < ActiveRecord::Base
800
+ class Post < ApplicationRecord
801
801
has_many :comments, dependent: :destroy
802
802
end
803
803
----
@@ -1046,7 +1046,7 @@ Enforce default values in the migrations themselves instead of in the applicatio
1046
1046
[source,ruby]
1047
1047
----
1048
1048
# bad - application enforced default value
1049
- class Product < ActiveRecord::Base
1049
+ class Product < ApplicationRecord
1050
1050
def amount
1051
1051
self[:amount] || 0
1052
1052
end
0 commit comments