ActiveRecord callbacks not called


#1

Hi, there must be something wrong somewhere in my unit test because
there’s a model I can’t have any callback executed on.

In my unit test I do
@image = Image.new
@image.set_uploaded_file({:file =>
uploaded_file(“pic1.jpg”,“image/jpeg”,“article-picture.jpg”),
:article_id => @article.id,
:title => ‘Titre test’
})
breakpoint
assert @image.save

When breakpointing, and trying @image.save manually I get a “true” as a
result. So I presume everything went fine.

The model is :
class Image < ActiveRecord::Base
belongs_to :article
before_destroy :on_before_destroy
after_save :on_after_save
validates_presence_of :extension

#cutted code

def on_after_save
breakpoint
end

end

The breakpoint in on_after_save is never reached.

I’m pulling my hairs out since yesterday - Any idea ?

Thanks


#2

I’m still having this problem, any idea ?


#3

Nuno wrote:

Hi, there must be something wrong somewhere in my unit test because
there’s a model I can’t have any callback executed on.

In my unit test I do
@image = Image.new
@image.set_uploaded_file({:file =>
uploaded_file(“pic1.jpg”,“image/jpeg”,“article-picture.jpg”),
:article_id => @article.id,
:title => ‘Titre test’
})
breakpoint
assert @image.save

When breakpointing, and trying @image.save manually I get a “true” as a
result. So I presume everything went fine.

The model is :
class Image < ActiveRecord::Base
belongs_to :article
before_destroy :on_before_destroy
after_save :on_after_save
validates_presence_of :extension

#cutted code

def on_after_save
breakpoint
end

end

The breakpoint in on_after_save is never reached.

I’m pulling my hairs out since yesterday - Any idea ?

I think you just do it like this:

class Image < ActiveRecord::Base
belongs_to :article
validates_presence_of :extension

#cutted code

def after_save
breakpoint
end

end

–Al Evans