Ticket #9775: foreign_key_on_migrations_with_polymorphic_and_docs.patch
| File foreign_key_on_migrations_with_polymorphic_and_docs.patch, 2.2 kB (added by tom, 1 year ago) |
|---|
-
activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
old new 350 350 # 351 351 # There's a short-hand method for each of the type values declared at the top. And then there's 352 352 # TableDefinition#timestamps that'll add created_at and updated_at as datetimes. 353 # 354 # TableDefinition#foreign_key will add an appropriately-named _id column, plus a corresponding _type 355 # column if the :polymorphic option is supplied. If :polymorphic is a hash of options, these will be 356 # used when creating the _type column. So what can be written like this: 357 # 358 # create_table :taggings do |t| 359 # t.integer :tag_id, :tagger_id, :taggable_id 360 # t.string :tagger_type 361 # t.string :taggable_type, :default => 'Photo' 362 # end 363 # 364 # Can also be written as follows using foreign_key: 365 # 366 # create_table :taggings do |t| 367 # t.foreign_key :tag 368 # t.foreign_key :tagger, :polymorphic => true 369 # t.foreign_key :taggable, :polymorphic => { :default => 'Photo' } 370 # end 353 371 def column(name, type, options = {}) 354 372 column = self[name] || ColumnDefinition.new(@base, name, type) 355 373 column.limit = options[:limit] || native[type.to_sym][:limit] if options[:limit] or native[type.to_sym] … … 376 394 column(:created_at, :datetime) 377 395 column(:updated_at, :datetime) 378 396 end 397 398 def foreign_key(*args) 399 options = args.extract_options! 400 polymorphic = options.delete(:polymorphic) 401 args.each do |col| 402 column("#{col}_id", :integer, options) 403 unless polymorphic.nil? 404 column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : {}) 405 end 406 end 407 end 408 alias :fkey :foreign_key 379 409 380 410 # Returns a String whose contents are the column definitions 381 411 # concatenated together. This string can then be pre and appended to