Intermittent Problem Persisting HABTM relationships

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 = in_file.sponsors << sponsor

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.


Steve and Jonathan

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.