简体   繁体   English

添加对现有 Rails 模型的模型引用

[英]Adding a Model Reference to existing Rails model

I'd like to know the "proper" way to approach adding a relation between two existing classes in Rails 3.我想知道在 Rails 3 中的两个现有类之间添加关系的“正确”方法。

Given existing models: Clown & Rabbit鉴于现有模型:小丑和兔子

I'd like to add a reference (belongs_to) from Rabbit to Clown.我想添加一个从 Rabbit 到 Clown 的引用 (belongs_to)。 I start by trying to generate a migration:我首先尝试生成迁移:

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:migrate on this migration I examine SQLite3's development.db and see a new column: "clown" referencerake db:migrate在这次迁移之后,我检查了 SQLite3 的 development.db 并看到一个新列: "clown" reference

I guess I was expecting a "clown_id" integer column 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).我确信 :reference 应该等同于“t.references :clown”,但我找不到文档(大惊喜)。 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. 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. 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 reference to :reference. ...没有参考:参考。

If you are using edge rails (4.0) you can use:如果您使用的是边轨 (4.0),您可以使用:

rails generate migration AddAddressRefToContacts address:references

As you can see by the docs .正如您在文档中看到的那样。

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

EDIT: See Paulo's answer below for a more updated answer (Rails 4+)编辑:有关更新的答案,请参阅下面 Paulo 的答案(Rails 4+)

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 .我不确定你是从哪里得到这个想法的,但是没有(也从来没有)这样的语法可以用add_column做你想做的add_column To get the behavior you want, you'd have to do t.refences :clown , as you stated.要获得您想要的行为,您必须执行t.refences :clown ,正如您所说。 In the background this will call: @base.add_column(@table_name, "#{col}_id", :integer, options) .在后台这将调用: @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.reference and assumed it was a datatype because calls such as t.integer and t.string exist, and those are datatypes.您看到了方法调用t.reference并假定它是一种数据类型,因为存在诸如t.integert.string调用,而这些都是数据类型。 That's wrong.那是错误的。 Reference isn't a datatype, it's just simply the name of a method, similar to t.rename is.引用不是数据类型,它只是一个方法的名称,类似于t.rename

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM