aboutsummaryrefslogtreecommitdiffstats
path: root/activerecord/lib
diff options
context:
space:
mode:
authorAndrey Novikov <envek@envek.name>2014-07-19 23:38:38 +0400
committerAndrey Novikov <envek@envek.name>2014-07-22 15:21:11 +0400
commit6d327dbc0cd7969375e14a56c0a9bc6a7c847056 (patch)
treef4d231d0918336f1b5b5afa4e7d7ee83bc3f4db2 /activerecord/lib
parentbb3b2d0b092af3c311ca06c5481b45ee6ee1b5b1 (diff)
downloadrails-6d327dbc0cd7969375e14a56c0a9bc6a7c847056.tar.gz
rails-6d327dbc0cd7969375e14a56c0a9bc6a7c847056.tar.bz2
rails-6d327dbc0cd7969375e14a56c0a9bc6a7c847056.zip
Allow to specify a type for foreign key column in migrations
[Andrey Novikov & Ɓukasz Sarnacki]
Diffstat (limited to 'activerecord/lib')
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb9
-rw-r--r--activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb3
2 files changed, 10 insertions, 2 deletions
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
index 98e6795f10..d51bf022a4 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
@@ -280,12 +280,19 @@ module ActiveRecord
column(:updated_at, :datetime, options)
end
+ # Adds an appropriately-named _id column as <tt>:integer</tt> (or whatever <tt>:type</tt> option specifies),
+ # plus a corresponding _type column if the <tt>:polymorphic</tt> option is supplied. If <tt>:polymorphic</tt>
+ # is a hash of options, these will be used when creating the <tt>_type</tt> column. The <tt>:index</tt> option
+ # will also create an index, similar to calling <tt>add_index</tt>.
+ #
+ # references :tagger, polymorphic: true, index: true, type: :uuid
def references(*args)
options = args.extract_options!
polymorphic = options.delete(:polymorphic)
index_options = options.delete(:index)
+ type = options.delete(:type) || :integer
args.each do |col|
- column("#{col}_id", :integer, options)
+ column("#{col}_id", type, options)
column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
index(polymorphic ? %w(id type).map { |t| "#{col}_#{t}" } : "#{col}_id", index_options.is_a?(Hash) ? index_options : {}) if index_options
end
diff --git a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
index 5814c2b711..8ecb7c0506 100644
--- a/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
+++ b/activerecord/lib/active_record/connection_adapters/abstract/schema_statements.rb
@@ -619,7 +619,8 @@ module ActiveRecord
def add_reference(table_name, ref_name, options = {})
polymorphic = options.delete(:polymorphic)
index_options = options.delete(:index)
- add_column(table_name, "#{ref_name}_id", :integer, options)
+ type = options.delete(:type) || :integer
+ add_column(table_name, "#{ref_name}_id", type, options)
add_column(table_name, "#{ref_name}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : options) if polymorphic
add_index(table_name, polymorphic ? %w[id type].map{ |t| "#{ref_name}_#{t}" } : "#{ref_name}_id", index_options.is_a?(Hash) ? index_options : {}) if index_options
end