Ruby on rails rake db:尝试填充外键字段时出现种子抛出属性未找到错误
我正在尝试在我的应用程序中建立关系,以便:Ruby on rails rake db:尝试填充外键字段时出现种子抛出属性未找到错误,ruby-on-rails,Ruby On Rails,我正在尝试在我的应用程序中建立关系,以便: a broadcast has 0..1 post and 0..1 jobs 我已经研究了属于并且有一个并且我已经尝试实现了这些,我在我的应用程序中有其他这样做的例子,这些例子都很有效,所以我无法理解为什么没有 以下是我的帖子和作业表格 class CreateJobs < ActiveRecord::Migration def change create_table :jobs do |t| t.text :titl
a broadcast has 0..1 post and 0..1 jobs
我已经研究了属于
并且有一个
并且我已经尝试实现了这些,我在我的应用程序中有其他这样做的例子,这些例子都很有效,所以我无法理解为什么没有
以下是我的帖子
和作业
表格
class CreateJobs < ActiveRecord::Migration
def change
create_table :jobs do |t|
t.text :title, null: :no #Must have a title
t.string :content, default: 'No content specified'
t.references :broadcast, null: :no, index: true # Must have been broadcast
t.timestamps
end
end
end
class CreatePosts < ActiveRecord::Migration
def change
create_table :posts do |t|
t.text :title, null: :no #Must have a title
t.string :content, default: 'No content specified'
t.references :broadcast, null: :no, index: true # Must have been broadcast
t.timestamps # Created at will double up as post date
end
end
end
这是db:migrate
create_table "broadcasts", force: true do |t|
t.text "title"
t.text "content"
t.boolean "jobs", default: false
t.boolean "general", default: false
t.integer "user_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "jobs", ["broadcast_id"], name: "index_jobs_on_broadcast_id"
create_table "posts", force: true do |t|
t.text "title"
t.string "content", default: "No content specified"
t.integer "broadcast_id"
t.datetime "created_at"
t.datetime "updated_at"
end
add_index "posts", ["broadcast_id"], name: "index_posts_on_broadcast_id"
create_table "user_details", force: true do |t|
t.string "login", limit: 40
t.string "salt", limit: 40
t.string "crypted_password", limit: 40
t.integer "user_id"
end
这是seeds.rb
文件
broadcast = Broadcast.create!(title: 'seed title 1 Broadcast', content: 'seeded Broadcast', jobs: true, general: true,
user: user)
Post.create!(title: 'seed title 1', content: "#{broadcast.content}", broadcast: broadcast)
broadcast2 = Broadcast.create!(title: 'seed title 2 Broadcast', content: 'Another seeded broadcast', general: true,
user: user)
broadcast3 = Broadcast.create!(title: 'seed title 3 Broadcast', content: 'Another seeded broadcast', jobs: true,
user: user)
Post.create!(title: 'seed title 2', content: "#{broadcast2.content}", broadcast: broadcast2)
Job.create!(title: 'seed title 1 job', content: "#{broadcast.content}", broadcast: broadcast2)
Job.create!(title: 'seed title 2 job', content: "#{broadcast2.content}", broadcast: broadcast3)
这是从rake db:seed抛出的错误消息
我已经在SQL资源管理器中检查了数据库,并且表确实存在,我不确定为什么这个错误会持续出现
rake aborted!
ActiveRecord::UnknownAttributeError: unknown attribute: broadcast
在这一行:
Post.create!(title: 'seed title 1', content: "#{broadcast.content}", broadcast: broadcast)
如果你需要更多的代码,就问我。毫无疑问,我遗漏了一些明显的东西,但这样做在我的其他项目中起了作用
谢谢,
克里斯。你的代码应该行得通。.我不知道为什么协会不按你的方式接受
广播
对象。应该可以。你的广播
迁移在哪里。。我的电脑出了故障,我重置了我的机器,现在它可以工作了。很好。。很好!!。。。。。。。。。。
Post.create!(title: 'seed title 1', content: "#{broadcast.content}", broadcast: broadcast)