Forum: Ruby on Rails Save Cascades Broken?

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Matt (Guest)
on 2007-06-08 00:35
(Received via mailing list)
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.
Jacob A. (Guest)
on 2007-06-08 01:06
(Received via mailing list)
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.
Matt (Guest)
on 2007-06-08 01:29
(Received via mailing list)
> 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?
Morgan Whitney (Guest)
on 2007-06-08 05:25
(Received via mailing list)
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> wrote:
> stop-gap until the relevant functionality is built in? Or even a bug?
>
>
> >
>


--
F. Morgan Whitney
http://www.blizzo.com
Matt (Guest)
on 2007-06-08 14:18
(Received via mailing list)
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!!!):
http://api.rubyonrails.org/classes/ActiveRecord/As...

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.
This topic is locked and can not be replied to.