Ticket #9775: references_on_migrations_with_polymorphic_and_docs.patch
| File references_on_migrations_with_polymorphic_and_docs.patch, 2.2 kB (added by arthurgeek, 1 year ago) |
|---|
-
activerecord/lib/active_record/connection_adapters/abstract/schema_definitions.rb
old new 393 393 # 394 394 # There's a short-hand method for each of the type values declared at the top. And then there's 395 395 # TableDefinition#timestamps that'll add created_at and updated_at as datetimes. 396 # 397 # TableDefinition#references will add an appropriately-named _id column, plus a corresponding _type 398 # column if the :polymorphic option is supplied. If :polymorphic is a hash of options, these will be 399 # used when creating the _type column. So what can be written like this: 400 # 401 # create_table :taggings do |t| 402 # t.integer :tag_id, :tagger_id, :taggable_id 403 # t.string :tagger_type 404 # t.string :taggable_type, :default => 'Photo' 405 # end 406 # 407 # Can also be written as follows using references: 408 # 409 # create_table :taggings do |t| 410 # t.references :tag 411 # t.references :tagger, :polymorphic => true 412 # t.references :taggable, :polymorphic => { :default => 'Photo' } 413 # end 396 414 def column(name, type, options = {}) 397 415 column = self[name] || ColumnDefinition.new(@base, name, type) 398 416 column.limit = options[:limit] || native[type.to_sym][:limit] if options[:limit] or native[type.to_sym] … … 419 437 column(:created_at, :datetime) 420 438 column(:updated_at, :datetime) 421 439 end 440 441 def references(*args) 442 options = args.extract_options! 443 polymorphic = options.delete(:polymorphic) 444 args.each do |col| 445 column("#{col}_id", :integer, options) 446 unless polymorphic.nil? 447 column("#{col}_type", :string, polymorphic.is_a?(Hash) ? polymorphic : {}) 448 end 449 end 450 end 451 alias :belongs_to :references 422 452 423 453 # Returns a String whose contents are the column definitions 424 454 # concatenated together. This string can then be pre and appended to