makandra / makandra-rubocop

makandra's default Rubocop configuration
MIT License
6 stars 1 forks source link

Discussion: Rails/HasManyOrHasOneDependent #13

Closed FLeinzi closed 4 years ago

FLeinzi commented 4 years ago

https://www.rubydoc.info/gems/rubocop-rails/RuboCop/Cop/Rails/HasManyOrHasOneDependent

app/models/user.rb:23:3: C: Rails/HasManyOrHasOneDependent: Specify a :dependent option.
  has_many :notes, inverse_of: :user
  ^^^^^^^^

Examples:

# 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