# Save Cascades Broken?

**URL:** <https://www.ruby-forum.com/t/save-cascades-broken/106263>\
**Category:** Rails\
**Created:** [June 7, 2007, 10:35pm UTC](https://www.ruby-forum.com/t/save-cascades-broken/106263 "2007-06-07T22:35:37Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [June 7, 2007, 10:35pm UTC](https://www.ruby-forum.com/t/save-cascades-broken/106263/1 "2007-06-07T22:35:37Z")

</div>

In a has\_many and belongs\_to association between 2 tables (Album and  
AlbumTracks for argyments sake) I create a new AlbumTrack Object, and  
append it to the Album.album\_tracks array like so:

album.album\_tracks \<\< AlbumTrack.new(params)

The annoying thing is, it is automatically saving the AlbumTrack, with  
I dont not want to happen until I explicitly call save on either that  
track, or the album that it belongs to.

Surely this can’t be the correct behaviour? If so, how on earth do I  
disable it.

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [June 7, 2007, 11:06pm UTC](https://www.ruby-forum.com/t/save-cascades-broken/106263/2 "2007-06-07T23:06:41Z")

</div>

Matt wrote:

> Surely this can’t be the correct behaviour? If so, how on earth do I  
> disable it.

That’s the way it’s implemented. Besides, how are you going to call save  
on an object you have no reference to (the AlbumTrack object)?

What you can do instead is:

album\_track = AlbumTrack.new(params.merge(:album =\> album))

Beware you will have to save the track and reload you album object  
before the new track is listed in album.album\_tracks.

If you change your mind and wan’t it to be saved at once anyway a clean  
way to do this is:

album.album\_tracks.create(params)

–  
Cheers,

- Jacob A.

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [June 7, 2007, 11:29pm UTC](https://www.ruby-forum.com/t/save-cascades-broken/106263/3 "2007-06-07T23:29:44Z")

</div>

> That’s the way it’s implemented. Besides, how are you going to call  
> save  
> on an object you have no reference to (the AlbumTrack object)?

I assumed that as the object was being stored within the parent object  
under the album\_tracks array which is referenced with has\_many that  
rails would have taken care of that for me?

Do you know if it is intentionally implemented this way, or if it is a  
stop-gap until the relevant functionality is built in? Or even a bug?

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [June 8, 2007, 3:25am UTC](https://www.ruby-forum.com/t/save-cascades-broken/106263/4 "2007-06-08T03:25:05Z")

</div>

To me it seems that for an Album object, the tracks contained within  
the album are part of the object. I am wondering if maybe you mean to  
do this:

#create the album, ad simultaneously save it with no tracks  
album = Album.create()

#add tracks, remove tracks, edit tracks, etc to your hearts content

#when you’re done and you actually want to save it…  
album.save

maybe you could explain more thoroughly what you are trying to do if  
that doesn’t help

On 6/7/07, Matt [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> stop-gap until the relevant functionality is built in? Or even a bug?
> 
> >

–  
F. Morgan Whitney

> **[Home - Blizzo](http://www.blizzo.com)**
>
> Insight. Culture. Passion. OUR STORY About Our Clients We recognize that our clients are the key element in our success and that we play an important part in helping build their business and brand. Whether our client is a small service business or a...

---

<div class="post-metadata">

**Author:** ![matt](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/m/a6a055/32.png) [@matt](https://www.ruby-forum.com/u/matt)\
**Post date:** [June 8, 2007, 12:18pm UTC](https://www.ruby-forum.com/t/save-cascades-broken/106263/5 "2007-06-08T12:18:55Z")

</div>

Thats exactly what I want to do. And it works fine for newly created  
Albums.

However, when editing an Album, if I add a track to the object, it is  
automatically saving the track to the database - even though I have  
saved neither it, nor the parent.

Since posting, I have managed to find the relevant section in the API  
docs (why on earth is it so difficult to use the API docs!!!):

> **[ActiveRecord::Associations::ClassMethods](https://api.rubyonrails.org/classes/ActiveRecord/Associations/ClassMethods.html)**
>
> Associations are a set of macro-like class methods for tying objects together through foreign keys.

It mentions this behaviour there, but it just seems to be the wrong  
way of doing things - just because I create a new object and append it  
in userspace doesnt neccessarily mean i want it stored in the  
database.

Apparently calling build() on the collection -  
Album.album\_tracks.build(params) - will achieve the results I want  
(and does) - but this seems more of a workarounnd than correct  
behaviour.
