[Rails] after_save, after_create, before_save etc... multiple times launching ...bug???

evan anarchogeek at gmail.com
Wed Jan 26 19:45:53 GMT 2005


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 at free.fr
<hangonsoft at free.fr> wrote:
> i forget one thing...i'm running on ruby 1.8.2, webrick , mysql 4.1.9 and macosx
> 
> Selon hangonsoft at free.fr:
> 
> >
> > 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 at lists.rubyonrails.org
> > http://lists.rubyonrails.org/mailman/listinfo/rails
> >
> 
> _______________________________________________
> Rails mailing list
> Rails at lists.rubyonrails.org
> http://lists.rubyonrails.org/mailman/listinfo/rails
>


More information about the Rails mailing list