【发布时间】:2016-09-13 07:41:57
【问题描述】:
我有一个名为员工的模型。以下是我的迁移文件。
class CreateEmployees < ActiveRecord::Migration
def change
create_table :employees, id: false do |t|
t.string :name
t.string :password
t.string :role
t.primary_key :name
end
end
end
现在,我想创建一个名为“teamplayer”的模型,其列为“name”,它需要引用员工模型中的“name”列。和 'tl' 列 它独立于这个模型。以下是我的“teamplayer”迁移文件。
class CreateTeamplayers < ActiveRecord::Migration
def change
create_table :teamplayers, :id false do |t|
t.string :tl
t.string :name
end
end
end
在上述文件中,如何将'name'列引用到模型员工?那么如何在rails中实现外键。
【问题讨论】:
-
我不会创建没有 PK 的表。
标签: ruby-on-rails ruby ruby-on-rails-4 rails-activerecord