我正在尝试在我的应用程序中建立关系,以便:
a broadcast has 0..1 post and 0..1 jobs
我已经研究过belongs_to
和has_one
,我试图实现这些,我在我的应用程序中这样做的其他例子正在工作,所以我无法弄清楚为什么这不是。< / p>
这是我的posts
和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
这是关系的broadcast.rb
,job.rb
,post.rb
定义
class Post < ActiveRecord::Base
#A post makes no sense without a title and content, they must exits.
validates_presence_of :title, :content
belongs_to :broadcast
end
class Job < ActiveRecord::Base
#A job makes no sense without a title and content, they must exits.
validates_presence_of :title, :content
belongs_to :broadcast
end
class Broadcast < ActiveRecord::Base
# The only restriction is that broadcasts must have some data to
# be valid and an associated broadcaster (user_id)
validates_presence_of :content, :title
# 0..1 relationship between job/post and broadcast
has_one :post
has_one :job
end
这是schema.rb
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)
如果您需要更多代码,请问我。毫无疑问,我错过了一些明显的东西,但做这样的事情在我的其他项目中起作用。
谢谢, 克里斯。