# bad
class User < ActiveRecord::Base
has_many :comments
has_one :avatar
end
# good
class User < ActiveRecord::Base
has_many :comments, dependent: :restrict_with_exception
has_one :avatar, dependent: :destroy
has_many :patients, through: :appointments
end
There may be situations where it is difficult to decide which is the correct dependency, e.g. if we use has_many for additional associations like has_many :published_comments, -> { where(published: true)
On the other side there will be many associations where we do not want a dependency. Here we would have to add dependent: :nullify very often.
Which should be our preferred way? :+1: for keeping, :-1: for disabling the cop
https://www.rubydoc.info/gems/rubocop-rails/RuboCop/Cop/Rails/HasManyOrHasOneDependent
Examples:
There may be situations where it is difficult to decide which is the correct dependency, e.g. if we use
has_many
for additional associations likehas_many :published_comments, -> { where(published: true)
On the other side there will be many associations where we do not want a dependency. Here we would have to add
dependent: :nullify
very often.Which should be our preferred way? :+1: for keeping, :-1: for disabling the cop