Forum: Ruby on Rails ActiveRecord callbacks not called

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Nuno (Guest)
on 2006-04-01 12:59
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
Nuno (Guest)
on 2006-04-02 12:18
I'm still having this problem, any idea ?
Al E. (Guest)
on 2006-04-02 20:26
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
This topic is locked and can not be replied to.