# Intermittent Problem Persisting HABTM relationships

**URL:** <https://www.ruby-forum.com/t/intermittent-problem-persisting-habtm-relationships/140091>\
**Category:** Rails\
**Created:** [June 10, 2008, 7:33pm UTC](https://www.ruby-forum.com/t/intermittent-problem-persisting-habtm-relationships/140091 "2008-06-10T19:33:38Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 10, 2008, 7:33pm UTC](https://www.ruby-forum.com/t/intermittent-problem-persisting-habtm-relationships/140091/1 "2008-06-10T19:33:38Z")

</div>

We are having an intermittent problem with has\_and\_belongs\_to\_many  
where the join table record is not created. For example, we have a  
HABT relationship mapped on both sides of models InFile and Sponsor.  
Our code looks like this:

sponsor = Sponsor.find(sponsor\_id)  
in\_file = InFile.new(attributes)  
in\_file.sponsors \<\< sponsor  
in\_file.save

Occasionally the new join record in in\_files\_sponsors is not  
inserted. It takes a while for this to start happening, but once it  
starts happening it continues. We are using Rails 2.02 with Ruby  
1.8.5.

Thanks!

Steve and Jonathan

---

<div class="post-metadata">

**Author:** ![casper\_the\_ghost](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/c/58956e/32.png) [@casper\_the\_ghost](https://www.ruby-forum.com/u/casper_the_ghost)\
**Post date:** [June 11, 2008, 8:33am UTC](https://www.ruby-forum.com/t/intermittent-problem-persisting-habtm-relationships/140091/2 "2008-06-11T08:33:43Z")

</div>

Try something like this:

Sponsor.find(sponsor\_id).in\_files \<\< InFile.create(attributes)

I’m wondering if trying to make the association before the in\_file  
object has been saved is causing the problem. But I can’t explain why  
that would only cause an intermittent problem.

Aaron

On Jun 10, 11:29 am, “removed\_email\_address@domain.invalid” [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
