references (*args)
public
Adds a reference. Optionally adds a type column, if
:polymorphic option is provided. references
and belongs_to
are acceptable. The reference column
will be an integer by default, the :type option can be
used to specify a different type. A foreign key will be created if a foreign_key
option is passed.
t . references ( :user )
t . references ( :user , type : " string ")
t . belongs_to ( :supplier , polymorphic : true )
See SchemaStatements#add_reference
Show source # File activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb, line 317
def references(*args)
options = args.extract_options!
polymorphic = options.delete(:polymorphic)
index_options = options.delete(:index)
foreign_key_options = options.delete(:foreign_key)
type = options.delete(:type) || :integer
if polymorphic && foreign_key_options
raise ArgumentError, "Cannot add a foreign key on a polymorphic relation"
end
args.each do |col|
column("#{col}_id", type, options)
column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
index(polymorphic ? %(type id).map { |t| "#{col}_#{t}" } : "#{col}_id", index_options.is_a?(Hash) ? index_options : {}) if index_options
foreign_key(col.to_s.pluralize, foreign_key_options.is_a?(Hash) ? foreign_key_options : {}) if foreign_key_options
end
end 1Note See the end part of the docs on column for example uses.