用Rails协会保存记录
问题描述:
我一直在浏览Rails指南,但经过验证和迁移之后就陷入了关联。所以,我有以下模型工作和人员,一个人可以有很多工作。我知道现实中会有多对多的,但我试图首先解决这个问题。用Rails协会保存记录
class Job < ActiveRecord::Base
belongs_to :people
end
和
class Person < ActiveRecord::Base
has_many :jobs
end
这里的架构
ActiveRecord::Schema.define(:version => 20110108185924) do
create_table "jobs", :force => true do |t|
t.string "occupation"
t.boolean "like"
t.datetime "created_at"
t.datetime "updated_at"
t.integer "person_id"
end
create_table "people", :force => true do |t|
t.string "first_name"
t.string "last_name"
t.datetime "created_at"
t.datetime "updated_at"
end
end
是有一些我可以做以下j = Job.first; j.Person
?然后,这会让我访问与j关联的Person对象。我无法在guides.rubyonrails.org上找到它,尽管到目前为止一直在抓住迁移和验证方面非常有帮助。
感谢
PS,如果有占地面积这样的事情链接将是巨大的任何教程。
答
我不知道我是否有问题。但是看看这个模式,我应该说你正走在正确的轨道上。你面临的问题是什么?
另外,belongs_to实际上应该是belongs_to:person。也就是说,一份工作属于一个人,每个人都可以有很多工作。
如果我做j = Job.new; j.occ =“会计师”; j.like = false; j.person_id = 1; j.save然后我想通过去j.Person访问,我得到一个没有方法的错误。 – tshauck 2011-01-08 19:55:50