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. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Matt wrote:> 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)? 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 --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
> That''s the way it''s implemented. Besides, how are you going to callsave> 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? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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 <matt-5JC3FsxXPsVpLGFMi4vTTA@public.gmane.org> wrote:> > > 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? > > > > >-- F. Morgan Whitney http://www.blizzo.com --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
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/Associations/ClassMethods.html 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. On Jun 8, 2:23 am, "Morgan Whitney" <morganwhit...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> 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 <m...-5JC3FsxXPsVpLGFMi4vTTA@public.gmane.org> wrote: > > > > > > 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? > > -- > F. Morgan Whitneyhttp://www.blizzo.com--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---