【发布时间】:2012-02-12 23:34:18
【问题描述】:
这可能被视为rails 3 associations error 的转贴。但这是我无法理解的东西。但是好的......这是交易......我在这里问了一个与表模式有关的问题。帖子是what is bad in this table schema structure,查询是:
`CREATE TABLE "pages" (
"id" INTEGER PRIMARY KEY AUTOINCREMENT NOT NULL,
"title" varchar(255),
"body" varchar(255),
"author" varchar(255),
"email" varchar(255),
"reference" varchar(255),
"created_at" datetime,
"updated_at" datetime);`
有人建议我应该为作者创建单独的表,这就是我尝试并搞砸的。谁能告诉我这些关联应该如何与我的场景一起工作?我真的需要很好地了解 ROR 关联。
edit-1
这是我的页面迁移代码:
class CreatePages < ActiveRecord::Migration
def self.up
create_table :pages do |t|
t.string :title, :unique => true, :limit => 50
t.string :body, :limit =>255
t.string :email, :unique => true, :limit => 50
t.string :reference, :limit => 40
t.timestamps
end
end
def self.down
drop_table :pages
end
end
这是我的作者迁移:
class CreateAuthors < ActiveRecord::Migration
def change
create_table :authors do |t|
t.string :author, :unique => true, :null => false, :limit => 50
t.timestamps
end
end
end
【问题讨论】:
-
@MrDanA 是的,我看过了。但问题是我有一个与模型关联的表单。当我将作者表与页面表分开时,我遇到了一些错误,但我仍然无法纠正。你发给我的链接我真的想不通。
-
您遇到什么类型的错误?您发布的问题只是“关联应该如何工作”,而不是“我的表单出现错误”(这是一个不同的问题)。
-
@mad_programmer,您没有像我在回答中指出的那样将“author_id”添加到“pages”中。
-
@AlexD 好的,我会添加它。
标签: ruby-on-rails ruby ruby-on-rails-3 ruby-on-rails-3.1