我想知道在两个之间添加关系的“正确”方法existingRails 3 中的类。
给定现有模型:小丑和兔子
我想添加从兔子到小丑的引用(belongs_to)。我首先尝试生成迁移:
rails g migration AddClownToRabbits clown:reference
这给了我一个看起来像这样的迁移:
class AddClownToRabbits < ActiveRecord::Migration
def self.up
add_column :rabbits, :clown, :reference
end
def self.down
remove_column :rabbits, :clown
end
end
After rake db:migrate
在这次迁移中,我检查了 SQLite3 的development.db 并看到一个新列:"clown" reference
我想我在期待一个"clown_id" integer
列和迁移看起来像:
class AddClownToRabbits < ActiveRecord::Migration
def self.up
add_column :rabbits, :clown_id
end
def self.down
remove_column :rabbits, :clown_id
end
end
我确信 :reference 应该等同于“t.references :clown”,但我找不到文档(大惊喜)。 API说add_column:Instantiates a new column for the table. The type parameter is normally one of the migrations native types, which is one of the following: :primary_key, :string, :text, :integer, :float, :decimal, :datetime, :timestamp, :time, :date, :binary, :boolean.
...没有参考至:参考。