Class: RuboCop::Cop::Obsession::Rails::MigrationBelongsTo

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/obsession/rails/migration_belongs_to.rb

Overview

This cop checks for migrations that use ‘references` instead of `belongs_to`.

Instead of adding ‘references` in migrations, use the `belongs_to` alias. It reads nicer and is more similar to the `belongs_to` declarations that you find in model code.

Examples:


# bad
def change
  add_reference :blog_posts, :user
end

# good
def change
  add_belongs_to :blog_posts, :user
end

Instance Method Summary collapse

Instance Method Details

#on_send(node) ⇒ Object



33
34
35
36
37
38
39
# File 'lib/rubocop/cop/obsession/rails/migration_belongs_to.rb', line 33

def on_send(node)
  if add_reference?(node)
    add_offense(node, message: 'Use add_belongs_to instead of add_reference')
  elsif table_reference?(node)
    add_offense(node, message: 'Use t.belongs_to instead of t.references')
  end
end