@@ -175,11 +175,11 @@ Use nested routes to express better the relationship between Active Record model
175175
176176[source,ruby]
177177----
178- class Post < ActiveRecord::Base
178+ class Post < ApplicationRecord
179179 has_many :comments
180180end
181181
182- class Comment < ActiveRecord::Base
182+ class Comment < ApplicationRecord
183183 belongs_to :post
184184end
185185
@@ -398,7 +398,7 @@ Avoid altering Active Record defaults (table names, primary key, etc) unless you
398398[source,ruby]
399399----
400400# bad - don't do this if you can modify the schema
401- class Transaction < ActiveRecord::Base
401+ class Transaction < ApplicationRecord
402402 self.table_name = 'order'
403403 ...
404404end
@@ -412,7 +412,7 @@ lead to broken code.
412412
413413[source,ruby]
414414----
415- class Transaction < ActiveRecord::Base
415+ class Transaction < ApplicationRecord
416416 # bad - implicit values - ordering matters
417417 enum type: %i[credit debit]
418418
@@ -430,7 +430,7 @@ Group macro-style methods (`has_many`, `validates`, etc) in the beginning of the
430430
431431[source,ruby]
432432----
433- class User < ActiveRecord::Base
433+ class User < ApplicationRecord
434434 # keep the default scope first (if any)
435435 default_scope { where(active: true) }
436436
@@ -475,26 +475,26 @@ Using `has_many :through` allows additional attributes and validations on the jo
475475[source,ruby]
476476----
477477# not so good - using has_and_belongs_to_many
478- class User < ActiveRecord::Base
478+ class User < ApplicationRecord
479479 has_and_belongs_to_many :groups
480480end
481481
482- class Group < ActiveRecord::Base
482+ class Group < ApplicationRecord
483483 has_and_belongs_to_many :users
484484end
485485
486486# preferred way - using has_many :through
487- class User < ActiveRecord::Base
487+ class User < ApplicationRecord
488488 has_many :memberships
489489 has_many :groups, through: :memberships
490490end
491491
492- class Membership < ActiveRecord::Base
492+ class Membership < ApplicationRecord
493493 belongs_to :user
494494 belongs_to :group
495495end
496496
497- class Group < ActiveRecord::Base
497+ class Group < ApplicationRecord
498498 has_many :memberships
499499 has_many :users, through: :memberships
500500end
@@ -626,7 +626,7 @@ Use named scopes freely.
626626
627627[source,ruby]
628628----
629- class User < ActiveRecord::Base
629+ class User < ApplicationRecord
630630 scope :active, -> { where(active: true) }
631631 scope :inactive, -> { where(active: false) }
632632
@@ -641,7 +641,7 @@ Arguably you can define even simpler scopes like this.
641641
642642[source,ruby]
643643----
644- class User < ActiveRecord::Base
644+ class User < ApplicationRecord
645645 def self.with_orders
646646 joins(:orders).select('distinct(users.id)')
647647 end
@@ -788,12 +788,12 @@ Define the `dependent` option to the `has_many` and `has_one` associations.
788788[source,ruby]
789789----
790790# bad
791- class Post < ActiveRecord::Base
791+ class Post < ApplicationRecord
792792 has_many :comments
793793end
794794
795795# good
796- class Post < ActiveRecord::Base
796+ class Post < ApplicationRecord
797797 has_many :comments, dependent: :destroy
798798end
799799----
@@ -1042,7 +1042,7 @@ Enforce default values in the migrations themselves instead of in the applicatio
10421042[source,ruby]
10431043----
10441044# bad - application enforced default value
1045- class Product < ActiveRecord::Base
1045+ class Product < ApplicationRecord
10461046 def amount
10471047 self[:amount] || 0
10481048 end
0 commit comments