@@ -179,11 +179,11 @@ Use nested routes to express better the relationship between Active Record model
179179
180180[source,ruby]
181181----
182- class Post < ActiveRecord::Base
182+ class Post < ApplicationRecord
183183 has_many :comments
184184end
185185
186- class Comment < ActiveRecord::Base
186+ class Comment < ApplicationRecord
187187 belongs_to :post
188188end
189189
@@ -402,7 +402,7 @@ Avoid altering Active Record defaults (table names, primary key, etc) unless you
402402[source,ruby]
403403----
404404# bad - don't do this if you can modify the schema
405- class Transaction < ActiveRecord::Base
405+ class Transaction < ApplicationRecord
406406 self.table_name = 'order'
407407 ...
408408end
@@ -416,7 +416,7 @@ lead to broken code.
416416
417417[source,ruby]
418418----
419- class Transaction < ActiveRecord::Base
419+ class Transaction < ApplicationRecord
420420 # bad - implicit values - ordering matters
421421 enum type: %i[credit debit]
422422
@@ -434,7 +434,7 @@ Group macro-style methods (`has_many`, `validates`, etc) in the beginning of the
434434
435435[source,ruby]
436436----
437- class User < ActiveRecord::Base
437+ class User < ApplicationRecord
438438 # keep the default scope first (if any)
439439 default_scope { where(active: true) }
440440
@@ -479,26 +479,26 @@ Using `has_many :through` allows additional attributes and validations on the jo
479479[source,ruby]
480480----
481481# not so good - using has_and_belongs_to_many
482- class User < ActiveRecord::Base
482+ class User < ApplicationRecord
483483 has_and_belongs_to_many :groups
484484end
485485
486- class Group < ActiveRecord::Base
486+ class Group < ApplicationRecord
487487 has_and_belongs_to_many :users
488488end
489489
490490# preferred way - using has_many :through
491- class User < ActiveRecord::Base
491+ class User < ApplicationRecord
492492 has_many :memberships
493493 has_many :groups, through: :memberships
494494end
495495
496- class Membership < ActiveRecord::Base
496+ class Membership < ApplicationRecord
497497 belongs_to :user
498498 belongs_to :group
499499end
500500
501- class Group < ActiveRecord::Base
501+ class Group < ApplicationRecord
502502 has_many :memberships
503503 has_many :users, through: :memberships
504504end
@@ -630,7 +630,7 @@ Use named scopes freely.
630630
631631[source,ruby]
632632----
633- class User < ActiveRecord::Base
633+ class User < ApplicationRecord
634634 scope :active, -> { where(active: true) }
635635 scope :inactive, -> { where(active: false) }
636636
@@ -645,7 +645,7 @@ Arguably you can define even simpler scopes like this.
645645
646646[source,ruby]
647647----
648- class User < ActiveRecord::Base
648+ class User < ApplicationRecord
649649 def self.with_orders
650650 joins(:orders).select('distinct(users.id)')
651651 end
@@ -792,12 +792,12 @@ Define the `dependent` option to the `has_many` and `has_one` associations.
792792[source,ruby]
793793----
794794# bad
795- class Post < ActiveRecord::Base
795+ class Post < ApplicationRecord
796796 has_many :comments
797797end
798798
799799# good
800- class Post < ActiveRecord::Base
800+ class Post < ApplicationRecord
801801 has_many :comments, dependent: :destroy
802802end
803803----
@@ -1046,7 +1046,7 @@ Enforce default values in the migrations themselves instead of in the applicatio
10461046[source,ruby]
10471047----
10481048# bad - application enforced default value
1049- class Product < ActiveRecord::Base
1049+ class Product < ApplicationRecord
10501050 def amount
10511051 self[:amount] || 0
10521052 end
0 commit comments