hangonsoft-GANU6spQydw@public.gmane.org
2005-Jan-26 17:01 UTC
after_save, after_create, before_save etc... multiple times launching ...bug???
hi, i have 2 tables (rooms & exams) linked by a intermediate table (has and belongs to many relation) when an new exam is created i want it to be validated for every room so inside exam model file i wrote: before_create :linkeveryroom private def linkeveryroom self.rooms<<Room.find_all end (or something like that i''m not in front of my Mac) it seems to work BUT the association is done 3 to 6 times (depends on what i use after-save or after_create or even with before)...each linking is present multiple times in the intermediate table (exams_rooms) someone experiencing the same behaviours??? hangon
hangonsoft-GANU6spQydw@public.gmane.org
2005-Jan-26 17:05 UTC
Re: after_save, after_create, before_save etc... multiple times launching ...bug???
i forget one thing...i''m running on ruby 1.8.2, webrick , mysql 4.1.9 and macosx Selon hangonsoft-GANU6spQydw@public.gmane.org:> > hi, > > i have 2 tables (rooms & exams) linked by a intermediate table (has and > belongs > to many relation) > > > when an new exam is created i want it to be validated for every room > > so inside exam model file i wrote: > > before_create :linkeveryroom > > private > def linkeveryroom > self.rooms<<Room.find_all > end > > (or something like that i''m not in front of my Mac) > > it seems to work BUT the association is done 3 to 6 times (depends on what i > use > after-save or after_create or even with before)...each linking is present > multiple times in the intermediate table (exams_rooms) > > someone experiencing the same behaviours??? > > > hangon > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
evan
2005-Jan-26 19:45 UTC
Re: after_save, after_create, before_save etc... multiple times launching ...bug???
I had the same problem. Both on my laptop ruby 1.8.2 osx and on our debian linux server. I''m sure it''s not connected to mysql versions or ruby versions, or os, as all three varried and the bug persisted. I spent a while looking at it, and i couldn''t figure it out. When i printed out the logs, and inserted breakpoints i could confirm that the callback before_create is only being called once. I think the problem lies somewhere in the order in which you call things, and how if you push one object on to another, then save, it can have unexpected side effects. Such as adding multiple times. The below works, i''m busy with other things and had to stop looking in to it once it got it working. def create_room if( self.rooms.size == 0 ) then room_data = { ''title'' => self.name, ''image_id'' => 1, ''description'' => self.name + "''s room''" } room = Room.new( room_data ) if room.save room.building<<(self) else #more needs to go there logger.error("failed to create room for new building") end logger.info( self.rooms.inspect ) end end On Wed, 26 Jan 2005 18:05:55 +0100, hangonsoft-GANU6spQydw@public.gmane.org <hangonsoft-GANU6spQydw@public.gmane.org> wrote:> i forget one thing...i''m running on ruby 1.8.2, webrick , mysql 4.1.9 and macosx > > Selon hangonsoft-GANU6spQydw@public.gmane.org: > > > > > hi, > > > > i have 2 tables (rooms & exams) linked by a intermediate table (has and > > belongs > > to many relation) > > > > > > when an new exam is created i want it to be validated for every room > > > > so inside exam model file i wrote: > > > > before_create :linkeveryroom > > > > private > > def linkeveryroom > > self.rooms<<Room.find_all > > end > > > > (or something like that i''m not in front of my Mac) > > > > it seems to work BUT the association is done 3 to 6 times (depends on what i > > use > > after-save or after_create or even with before)...each linking is present > > multiple times in the intermediate table (exams_rooms) > > > > someone experiencing the same behaviours??? > > > > > > hangon > > _______________________________________________ > > Rails mailing list > > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > > http://lists.rubyonrails.org/mailman/listinfo/rails > > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >