class RuboCop::Cop::Rails::HasManyOrHasOneDependent

This cop looks for `has_many` or `has_one` associations that don't specify a `:dependent` option. It doesn't register an offense if `:through` option was specified.

@example

# 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

Constants

MSG

Public Instance Methods

on_send(node) click to toggle source
# File lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb, line 49
def on_send(node)
  if !association_without_options?(node)
    return if valid_options?(association_with_options?(node))
  elsif with_options_block(node.parent)
    return if valid_options?(with_options_block(node.parent))
  end

  add_offense(node, location: :selector)
end

Private Instance Methods

valid_options?(options) click to toggle source
# File lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb, line 61
def valid_options?(options)
  return true unless options
  return true if options.any? do |o|
    dependent_option?(o) || present_option?(o)
  end

  false
end