# "xxx\_id may not be null" when saving many to many joined objects

**URL:** <https://www.ruby-forum.com/t/xxx-id-may-not-be-null-when-saving-many-to-many-joined-objects/148941>\
**Category:** Rails\
**Created:** [September 18, 2008, 3:03pm UTC](https://www.ruby-forum.com/t/xxx-id-may-not-be-null-when-saving-many-to-many-joined-objects/148941 "2008-09-18T15:03:35Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![pat.turner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e47774/32.png) [@pat.turner](https://www.ruby-forum.com/u/pat.turner)\
**Post date:** [September 18, 2008, 3:03pm UTC](https://www.ruby-forum.com/t/xxx-id-may-not-be-null-when-saving-many-to-many-joined-objects/148941/1 "2008-09-18T15:03:35Z")

</div>

Hi,

I have two model classes; Tune and Title and they are joined in a many  
to  
many relationship through a Variation class. Here’s some stub code of  
the  
models:

class Tune \< ActiveRecord::Base  
has\_many :variations, :dependent =\> :destroy  
has\_many :titles, :through =\> :variations  
…  
class Title \< ActiveRecord::Base  
has\_many :variations, :dependent =\> :destroy  
has\_many :tunes, :through =\> :variations  
…  
class Variation \< ActiveRecord::Base  
belongs\_to :tune  
belongs\_to :title  
…

In my TunesController I create a new Tune, Variation and Title. I set  
the  
:tune and :title attributes in the Variation, and add the Variation to  
the  
tune and title’s :variations arrays.

The problem is that when I save the Tune, I get the following error:  
SQLite3::SQLException: variations.title\_id may not be NULL:  
INSERT INTO “variations” (“updated\_at”, “title\_id”, “tune\_id”,  
“created\_at”)  
VALUES(‘2008-09-18 12:21:52’, NULL, NULL, ‘2008-09-18 12:21:52’)

I assumed that Rails would know that the Tune, Variation and Title are  
all  
new objects and perform the save of the Tune and Title, get their IDs to  
store in the Variation and lastly save the Variation.

Any help or pointers for where to look are greatly appreciated.

## Thanks, Pat

View this message in context:  
[http://www.nabble.com/"xxx\_id-may-not-be-null"-when-saving-many-to-many-joined-objects-tp19551721p19551721.html](http://www.nabble.com/%22xxx_id-may-not-be-null%22-when-saving-many-to-many-joined-objects-tp19551721p19551721.html)  
Sent from the RubyOnRails Users mailing list archive at [Nabble.com](http://Nabble.com).

---

<div class="post-metadata">

**Author:** ![pat.turner](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/e47774/32.png) [@pat.turner](https://www.ruby-forum.com/u/pat.turner)\
**Post date:** [September 18, 2008, 5:53pm UTC](https://www.ruby-forum.com/t/xxx-id-may-not-be-null-when-saving-many-to-many-joined-objects/148941/2 "2008-09-18T17:53:55Z")

</div>

Hi,

fixed the problem. I added the Variation to the ‘created’ Tune before  
I had added the Title to the Variation. So the Variation obviously  
didn’t have a Title object with which an id could be derived/created.

Cheers  
Pat
