我想知道“正确”的方式来处理Rails中加入3添加模型参考现有的Rails的模型
鉴于现有模型两种现有类之间的关系:小丑&兔
我想添加一个从兔子到小丑的参考(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
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
我确定:引用应该等同于“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.
...没有参考到:参考。
有关文档(以及它在文档中的引用,请参阅http://guides.rubyonrails.org/getting_started.html),然后向下滚动到7.1生成模型并查看该标题下的代码段。你会看到它是*:引用*而不是*:引用*。 – iconoclast 2011-04-15 02:38:32