Saving a Record with Rails Association

Posted by tshauck on Stack Overflow See other posts from Stack Overflow or by tshauck
Published on 2011-01-08T19:40:44Z Indexed on 2011/01/08 19:53 UTC
Read the original article Hit count: 159

Filed under:
|

Hi,

I've been going through the Rails Guides, but have gotten stuck on associations after going through validations and migrations. So, I have the following models Job and Person, where a Person can have many jobs. I know that in reality there'd be a many-to-many, but I'm trying to get my handle on this first.

class Job < ActiveRecord::Base  
  belongs_to :people
end

and

class Person < ActiveRecord::Base
  has_many :jobs
end

Here's the schema

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

Is there some I can do the following j = Job.first; j.Person? Then that'd give me access to the Person object associated with the j. I couldn't find it on guides.rubyonrails.org, although it has been very helpful getting a grip on migrations and validations thus far.

Thanks

PS, If there are any tutorials that covers more of this kind of things links would be great.

© Stack Overflow or respective owner

Related posts about ruby-on-rails

Related posts about activerecord