我正在学习rails并且正在做关联 - has_many,belongs_to等。声明这样的关系会在表中生成列吗?我正在使用一个MySQL数据库,并希望看到一个的东西 _id列,但是没有一个。
答案 0 :(得分:1)
这是创建列的迁移。以下是3个例子:
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
# This creates a field address_id
t.references :address
# This is equivalent as above
t.integer :address_id
# This special case for polymorphic association creates 2 fields
# address_id and address_type
t.references :address, polymorphic: true
end
end
end
在您的模型中,关联只告诉rails如何链接记录但不修改数据库
class User < ActiveRecord::Base
# Tells rails that users.address_id is linked to addresses.id
belongs_to: address
end