【发布时间】:2012-12-06 23:04:14
【问题描述】:
我一直在使用 Michael Hartl 的教程,并且正在尝试创建一个名为“Recipe”的新模型,以允许用户发布食谱。该模型本质上与微博模型相同,因为用户应该能够发布许多食谱,并且所有食谱都应该链接到一个用户。因此,我使用了与 micropost 相同的命令和迁移:
class CreateMicroposts < ActiveRecord::Migration
def change
create_table :microposts do |t|
t.string :content
t.integer :user_id
t.timestamps
end
add_index :microposts, [:user_id, :created_at]
end
end
使用命令创建:rails generate model Micropost content:string user_id:integer
我遇到的问题是两个表中的主键都是 user_id。这会起作用吗,或者当我尝试输入食谱时,它会尝试从微帖子表中提取吗?提前致谢
【问题讨论】:
-
对不起,我还要提一下:食谱的代码输入与上面相同:唯一的区别是它在顶部显示“CreateRecipes”而不是“CreateMicroposts”,并且它具有 create_table 方法它说的是食谱。索引在两者中是相同的