# Many-to-Many: Where to add the children?

**URL:** <https://www.ruby-forum.com/t/many-to-many-where-to-add-the-children/51292>\
**Category:** Rails\
**Created:** [December 29, 2005, 5:31am UTC](https://www.ruby-forum.com/t/many-to-many-where-to-add-the-children/51292 "2005-12-29T05:31:00Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [December 29, 2005, 5:31am UTC](https://www.ruby-forum.com/t/many-to-many-where-to-add-the-children/51292/1 "2005-12-29T05:31:00Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [December 29, 2005, 2:32pm UTC](https://www.ruby-forum.com/t/many-to-many-where-to-add-the-children/51292/2 "2005-12-29T14:32:50Z")

</div>

Jason F. wrote:

> # … 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.

In your real code Does Person have any other associations?  
There is a Active Record bug that causes join table entries for  
habtm relations on unsaved objects to not be added in some  
circumstances when other associations have been defined:  
[Problem with .save when model has two HABTM - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/48785)  
[Rails RC5 and saving associations - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/48816)

Try:

person = Person.create!  
person.add\_job(“teacher”)  
person.save!

–  
We develop, watch us RoR, in numbers too big to ignore.

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [December 29, 2005, 3:25pm UTC](https://www.ruby-forum.com/t/many-to-many-where-to-add-the-children/51292/3 "2005-12-29T15:25:25Z")

</div>

Mark Reginald J. wrote:

> In your real code Does Person have any other associations?

Yes, Person has the following relationships:

belongs\_to :family  
has\_many :kids  
has\_and\_belongs\_to\_many :organizations

Job is also in another many-to-many relationship:

has\_and\_belongs\_to\_many :companies

> There is a Active Record bug that causes join table entries for  
> habtm relations on unsaved objects to not be added in some  
> circumstances when other associations have been defined:  
> [Problem with .save when model has two HABTM - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/48785)  
> [Rails RC5 and saving associations - Rails - Ruby-Forum](http://www.ruby-forum.com/topic/48816)
> 
> Try:
> 
> person = Person.create!  
> person.add\_job(“teacher”)  
> person.save!

Thanks for the tip, I’ll read those articles and give this a shot!

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [December 29, 2005, 4:53pm UTC](https://www.ruby-forum.com/t/many-to-many-where-to-add-the-children/51292/4 "2005-12-29T16:53:35Z")

</div>

Mark Reginald J. wrote:

> This is now aparently fixed in trunk:  
> [http://dev.rubyonrails.org/changeset/3332](http://dev.rubyonrails.org/changeset/3332)

Thanks, again!

---

<div class="post-metadata">

**Author:** ![jfoxny](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/j/a6a055/32.png) [@jfoxny](https://www.ruby-forum.com/u/jfoxny)\
**Post date:** [December 29, 2005, 4:30pm UTC](https://www.ruby-forum.com/t/many-to-many-where-to-add-the-children/51292/5 "2005-12-29T16:30:54Z")

</div>

Jason F. wrote:

> Yes, Person has the following relationships:
> 
> belongs\_to :family  
> has\_many :kids  
> has\_and\_belongs\_to\_many :organizations
> 
> Job is also in another many-to-many relationship:
> 
> has\_and\_belongs\_to\_many :companies

Yes, two habtms is a bug trigger.

This is now aparently fixed in trunk:  
[http://dev.rubyonrails.org/changeset/3332](http://dev.rubyonrails.org/changeset/3332)

–  
We develop, watch us RoR, in numbers too big to ignore.
