Ruby-on-rails 添加对现有 Rails 模型的模型引用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/4338973/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Adding a Model Reference to existing Rails model
提问by Meltemi
I'd like to know the "proper" way to approach adding a relation between two existingclasses in Rails 3.
我想知道在 Rails 3 中的两个现有类之间添加关系的“正确”方法。
Given existing models: Clown & Rabbit
鉴于现有模型:小丑和兔子
I'd like to add a reference (belongs_to) from Rabbit to Clown. I start by trying to generate a migration:
我想添加一个从 Rabbit 到 Clown 的引用 (belongs_to)。我首先尝试生成迁移:
rails g migration AddClownToRabbits clown:reference
which gives me a migration that looks like:
这给了我一个看起来像的迁移:
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:migrateon this migration I examine SQLite3's development.db and see a new column: "clown" reference
在rake db:migrate这次迁移之后,我检查了 SQLite3 的 development.db 并看到了一个新列:"clown" reference
I guess I was expecting a "clown_id" integercolumn and a migration that looked like:
我想我期待一个"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
I'm sure :reference is supposed to be equivalent to "t.references :clown" but I can't find the documentation (big surprise). API says 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.
我确定 :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.
...with no referenceto :reference.
...没有参考:参考。
采纳答案by clemensp
After you set belongs_to in Rabbit, and has_many in Clown, you can do a migration with:
在Rabbit中设置belongs_to并在Clown中设置has_many后,您可以使用以下命令进行迁移:
add_column :rabbit, :clown_id, :integer
回答by Paulo Fidalgo
回答by ryeguy
I'm not sure where you got this idea, but there is no (and never has been) such syntax to do what you want with add_column. To get the behavior you want, you'd have to do t.refences :clown, as you stated. In the background this will call: @base.add_column(@table_name, "#{col}_id", :integer, options).
我不确定你是从哪里得到这个想法的,但是没有(也从来没有)这样的语法可以用add_column. t.refences :clown如您所说,要获得您想要的行为,您必须这样做。在后台,这将调用:@base.add_column(@table_name, "#{col}_id", :integer, options)。
See here.
见这里。
EDIT:
编辑:
I think I can see the source of your confusion. You saw the method call t.referenceand assumed it was a datatype because calls such as t.integerand t.stringexist, and those are datatypes. That's wrong. Reference isn't a datatype, it's just simply the name of a method, similar to t.renameis.
我想我可以看到你困惑的根源。您看到了方法调用t.reference并假定它是一种数据类型,因为存在诸如t.integer和 之类的调用t.string,而这些都是数据类型。那是错误的。引用不是数据类型,它只是一个方法的名称,类似于t.renameis。

