Rails 迁移:t.references 具有替代名称?

发布于 2024-09-04 03:42:54 字数 392 浏览 3 评论 0原文

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

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

但我希望它引用两个其他课程,例如:

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

我可以说以下吗?

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

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

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

but I want it to reference two other courses like:

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

Can I say the following?

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

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

素手挽清风 2024-09-11 03:42:54

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

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

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

t.references :transferable_as, index: true, foreign_key: {to_table: :courses}
t.references :same_as, index: true, foreign_key: {to_table: :courses}
七月上 2024-09-11 03:42:54

您可以这样做:

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

或使用 t.belongs_to 作为 t.references 的别名

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

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

在 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

You can do it this way:

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

or using t.belongs_to as an alias for t.references

You can't add foreign_key: true to 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:

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_table block like this:

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
傾城如夢未必闌珊 2024-09-11 03:42:54

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

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

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"
⒈起吃苦の倖褔 2024-09-11 03:42:54

我认为这个线程有一个不同的更 Rails 风格的方式:
脚手架 ActiveRecord:相同数据类型的两列

在迁移中:

t.belongs_to :transferrable_as

t.belongs_to :same_as

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

In the migration:

t.belongs_to :transferrable_as

t.belongs_to :same_as

我的影子我的梦 2024-09-11 03:42:54

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

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

I don't think references accepts the :as option, but you can create your columns manually...

create_table :courses do |t| 
  t.string  :name 
  t.integer :course1_id
  t.integer :course2_id 
  t.timestamps 
end 
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文