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.
on 2007-06-07 22:35
on 2007-06-07 23:06
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 Atzen
on 2007-06-07 23:29
> 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?
on 2007-06-08 03:25
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 <firstname.lastname@example.org> wrote: > stop-gap until the relevant functionality is built in? Or even a bug? > > > > > -- F. Morgan Whitney http://www.blizzo.com
on 2007-06-08 12:18
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.