I have two classes Person and Job defined as follows:
class Person < ActiveRecord::Base
:has_and_belongs_to_many jobs
def add_job(job_name)
job = Job.find_by_name(job_name) # See if this job exists
if job.nil? # If this job doesn't exist
job = Job.new # Create a new job
job.name = job_name # with the given name
end
jobs << job # Add to this person's jobs
end
end
class Job < ActiveRecord::Base
:has_and_belongs_to_many people
end
I also have a controller that uses the add_job method in Person as
follows:
… omitted code
person = Person.new
person.add_job(“teacher”)
person.save!
Now, when the job with the name “teacher” already exists, the
relationship between the new Person and the existing Job is not saved.
If the job does not exist, it saves the new Job and creates the
relationship. However, if I change the code of the add_job method as
follows:
def add_job(job_name)
job = Job.find_by_name(job_name) # See if this job exists
if job.nil? # If this job doesn’t exist
job = Job.new # Create a new job
job.name = job_name # with the given name
jobs << job # and add to this person’s jobs
else # Otherwise, if it does exist
job.people << self # Add this person to the job’s
people
end
end
It saves the relationship correctly regardless of whether or not the Job
already exists. My question is, why? – Why do I need to add the
person to the job’s people array to get the assocation to save when the
Job already exists?
person = Person.new
person.add_job(“teacher”)
person.save!
Now, when the job with the name “teacher” already exists, the
relationship between the new Person and the existing Job is not saved.
If the job does not exist, it saves the new Job and creates the
relationship.