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.
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.
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)?
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?
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!!!):
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.