@@ -175,11 +175,11 @@ Use nested routes to express better the relationship between Active Record model
175
175
176
176
[source,ruby]
177
177
----
178
- class Post < ActiveRecord::Base
178
+ class Post < ApplicationRecord
179
179
has_many :comments
180
180
end
181
181
182
- class Comment < ActiveRecord::Base
182
+ class Comment < ApplicationRecord
183
183
belongs_to :post
184
184
end
185
185
@@ -398,7 +398,7 @@ Avoid altering Active Record defaults (table names, primary key, etc) unless you
398
398
[source,ruby]
399
399
----
400
400
# bad - don't do this if you can modify the schema
401
- class Transaction < ActiveRecord::Base
401
+ class Transaction < ApplicationRecord
402
402
self.table_name = 'order'
403
403
...
404
404
end
@@ -412,7 +412,7 @@ lead to broken code.
412
412
413
413
[source,ruby]
414
414
----
415
- class Transaction < ActiveRecord::Base
415
+ class Transaction < ApplicationRecord
416
416
# bad - implicit values - ordering matters
417
417
enum type: %i[credit debit]
418
418
@@ -430,7 +430,7 @@ Group macro-style methods (`has_many`, `validates`, etc) in the beginning of the
430
430
431
431
[source,ruby]
432
432
----
433
- class User < ActiveRecord::Base
433
+ class User < ApplicationRecord
434
434
# keep the default scope first (if any)
435
435
default_scope { where(active: true) }
436
436
@@ -475,26 +475,26 @@ Using `has_many :through` allows additional attributes and validations on the jo
475
475
[source,ruby]
476
476
----
477
477
# not so good - using has_and_belongs_to_many
478
- class User < ActiveRecord::Base
478
+ class User < ApplicationRecord
479
479
has_and_belongs_to_many :groups
480
480
end
481
481
482
- class Group < ActiveRecord::Base
482
+ class Group < ApplicationRecord
483
483
has_and_belongs_to_many :users
484
484
end
485
485
486
486
# preferred way - using has_many :through
487
- class User < ActiveRecord::Base
487
+ class User < ApplicationRecord
488
488
has_many :memberships
489
489
has_many :groups, through: :memberships
490
490
end
491
491
492
- class Membership < ActiveRecord::Base
492
+ class Membership < ApplicationRecord
493
493
belongs_to :user
494
494
belongs_to :group
495
495
end
496
496
497
- class Group < ActiveRecord::Base
497
+ class Group < ApplicationRecord
498
498
has_many :memberships
499
499
has_many :users, through: :memberships
500
500
end
@@ -626,7 +626,7 @@ Use named scopes freely.
626
626
627
627
[source,ruby]
628
628
----
629
- class User < ActiveRecord::Base
629
+ class User < ApplicationRecord
630
630
scope :active, -> { where(active: true) }
631
631
scope :inactive, -> { where(active: false) }
632
632
@@ -641,7 +641,7 @@ Arguably you can define even simpler scopes like this.
641
641
642
642
[source,ruby]
643
643
----
644
- class User < ActiveRecord::Base
644
+ class User < ApplicationRecord
645
645
def self.with_orders
646
646
joins(:orders).select('distinct(users.id)')
647
647
end
@@ -788,12 +788,12 @@ Define the `dependent` option to the `has_many` and `has_one` associations.
788
788
[source,ruby]
789
789
----
790
790
# bad
791
- class Post < ActiveRecord::Base
791
+ class Post < ApplicationRecord
792
792
has_many :comments
793
793
end
794
794
795
795
# good
796
- class Post < ActiveRecord::Base
796
+ class Post < ApplicationRecord
797
797
has_many :comments, dependent: :destroy
798
798
end
799
799
----
@@ -1042,7 +1042,7 @@ Enforce default values in the migrations themselves instead of in the applicatio
1042
1042
[source,ruby]
1043
1043
----
1044
1044
# bad - application enforced default value
1045
- class Product < ActiveRecord::Base
1045
+ class Product < ApplicationRecord
1046
1046
def amount
1047
1047
self[:amount] || 0
1048
1048
end
0 commit comments