Skip to content

Commit

Permalink
Merge pull request #310 from andyw8/andyw8/use-application-record
Browse files Browse the repository at this point in the history
Replace references to `ActiveRecord::Base` with `ApplicationRecord`
  • Loading branch information
andyw8 authored Feb 6, 2022
2 parents 89c188e + 43b7590 commit ac89109
Showing 1 changed file with 15 additions and 15 deletions.
30 changes: 15 additions & 15 deletions README.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -175,11 +175,11 @@ Use nested routes to express better the relationship between Active Record model

[source,ruby]
----
class Post < ActiveRecord::Base
class Post < ApplicationRecord
has_many :comments
end
class Comment < ActiveRecord::Base
class Comment < ApplicationRecord
belongs_to :post
end
Expand Down Expand Up @@ -398,7 +398,7 @@ Avoid altering Active Record defaults (table names, primary key, etc) unless you
[source,ruby]
----
# bad - don't do this if you can modify the schema
class Transaction < ActiveRecord::Base
class Transaction < ApplicationRecord
self.table_name = 'order'
...
end
Expand All @@ -412,7 +412,7 @@ lead to broken code.

[source,ruby]
----
class Transaction < ActiveRecord::Base
class Transaction < ApplicationRecord
# bad - implicit values - ordering matters
enum type: %i[credit debit]
Expand All @@ -430,7 +430,7 @@ Group macro-style methods (`has_many`, `validates`, etc) in the beginning of the

[source,ruby]
----
class User < ActiveRecord::Base
class User < ApplicationRecord
# keep the default scope first (if any)
default_scope { where(active: true) }
Expand Down Expand Up @@ -475,26 +475,26 @@ Using `has_many :through` allows additional attributes and validations on the jo
[source,ruby]
----
# not so good - using has_and_belongs_to_many
class User < ActiveRecord::Base
class User < ApplicationRecord
has_and_belongs_to_many :groups
end
class Group < ActiveRecord::Base
class Group < ApplicationRecord
has_and_belongs_to_many :users
end
# preferred way - using has_many :through
class User < ActiveRecord::Base
class User < ApplicationRecord
has_many :memberships
has_many :groups, through: :memberships
end
class Membership < ActiveRecord::Base
class Membership < ApplicationRecord
belongs_to :user
belongs_to :group
end
class Group < ActiveRecord::Base
class Group < ApplicationRecord
has_many :memberships
has_many :users, through: :memberships
end
Expand Down Expand Up @@ -626,7 +626,7 @@ Use named scopes freely.

[source,ruby]
----
class User < ActiveRecord::Base
class User < ApplicationRecord
scope :active, -> { where(active: true) }
scope :inactive, -> { where(active: false) }
Expand All @@ -641,7 +641,7 @@ Arguably you can define even simpler scopes like this.

[source,ruby]
----
class User < ActiveRecord::Base
class User < ApplicationRecord
def self.with_orders
joins(:orders).select('distinct(users.id)')
end
Expand Down Expand Up @@ -788,12 +788,12 @@ Define the `dependent` option to the `has_many` and `has_one` associations.
[source,ruby]
----
# bad
class Post < ActiveRecord::Base
class Post < ApplicationRecord
has_many :comments
end
# good
class Post < ActiveRecord::Base
class Post < ApplicationRecord
has_many :comments, dependent: :destroy
end
----
Expand Down Expand Up @@ -1042,7 +1042,7 @@ Enforce default values in the migrations themselves instead of in the applicatio
[source,ruby]
----
# bad - application enforced default value
class Product < ActiveRecord::Base
class Product < ApplicationRecord
def amount
self[:amount] || 0
end
Expand Down

0 comments on commit ac89109

Please sign in to comment.