module RuboCop::Cop::ActiveRecordHelper

def resolve_relation_into_column(name:, class_node:, table:)

Returns:
  • (Array, String, nil) -

Parameters:
  • table (RuboCop::Rails::SchemaLoader::Table) --
  • class_node (RuboCop::AST::Node) --
  • name (String) --
def resolve_relation_into_column(name:, class_node:, table:)
  return unless table
  return name if table.with_column?(name: name)
  find_belongs_to(class_node) do |belongs_to|
    next unless belongs_to.first_argument.value.to_s == name
    fk = foreign_key_of(belongs_to) || "#{name}_id"
    next unless table.with_column?(name: fk)
    return polymorphic?(belongs_to) ? [fk, "#{name}_type"] : fk
  end
  nil
end