Ruby-on-rails Rails 迁移:t.references 具有替代名称?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2933582/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 22:42:56  来源:igfitidea点击:

Rails migration: t.references with alternative name?

ruby-on-railsassociationsmigration

提问by themirror

So I have a create_table like this for Courses at a School:

所以我有一个像这样的学校课程的 create_table :

create_table :courses do |t|
  t.string :name
  t.references :course
  t.timestamps
end

but I want it to reference twoother courses like:

但我想它引用2门其他课程,如:

has_many :transferrable_as # A Course
has_many :same_as          # Another Course

Can I say the following?

我可以说以下吗?

t.references :transferrable_as, :as=> :course

采纳答案by themirror

I think this thread has a different more Rails-ish way: Scaffolding ActiveRecord: two columns of the same data type

我认为这个线程有一个不同的更 Rails 的方式: Scaffolding ActiveRecord: two columns of the same data type

In the migration:

在迁移中:

t.belongs_to :transferrable_as

t.belongs_to :same_as

t.belongs_to :transferrable_as

t.belongs_to :same_as

回答by Ryan

You can do this all in the initial migration/column definition (at least currently in Rails 5):

您可以在初始迁移/列定义中完成这一切(至少目前在 Rails 5 中):

t.references :transferable_as, index: true, foreign_key: {to_table: :courses}
t.references :same_as, index: true, foreign_key: {to_table: :courses}

回答by Toby 1 Kenobi

You can do it this way:

你可以这样做:

create_table :courses do |t|
  t.string :name
  t.references :transferrable_as, references: :courses
  t.references :same_as, references: :courses
  t.timestamps
end

or using t.belongs_toas an alias for t.references

t.belongs_to用作别名t.references

You can't add foreign_key: trueto those two references lines. If you want to mark them as foreign keys at the database level you need to have a migration with this:

您不能添加foreign_key: true到这两个参考行。如果您想在数据库级别将它们标记为外键,您需要进行迁移:

add_foreign_key :courses, :courses, column: :transferrable_as_id
add_foreign_key :courses, :courses, column: :same_as_id

Update

更新

In Rails 5.1 and above you can add the foreign key in the migration in the create_tableblock like this:

在 Rails 5.1 及更高版本中,您可以在create_table块中的迁移中添加外键,如下所示:

create_table :courses do |t|
  t.string :name
  t.references :transferrable_as, foreign_key: { to_table: 'courses' }
  t.references :same_as, foreign_key: { to_table: 'courses' }
  t.timestamps
end

回答by Diego Diaz de Berenguer

As an added answer to this question -- the Model should have the following line to complete the association:

作为对这个问题的补充回答——模型应该有以下行来完成关联:

    belongs_to :transferrable_as, class_name: "Course"
    belongs_to :same_as, class_name: "Course"

回答by Ju Nogueira

I don't think referencesaccepts the :asoption, but you can create your columns manually...

我认为不references接受该:as选项,但您可以手动创建列...

create_table :courses do |t| 
  t.string  :name 
  t.integer :course1_id
  t.integer :course2_id 
  t.timestamps 
end