# Why callback does not work

**URL:** <https://www.ruby-forum.com/t/why-callback-does-not-work/210755>\
**Category:** Rails\
**Created:** [August 16, 2011, 6:41pm UTC](https://www.ruby-forum.com/t/why-callback-does-not-work/210755 "2011-08-16T18:41:59Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![dubstep](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/d/cdc98d/32.png) [@dubstep](https://www.ruby-forum.com/u/dubstep)\
**Post date:** [August 16, 2011, 6:41pm UTC](https://www.ruby-forum.com/t/why-callback-does-not-work/210755/1 "2011-08-16T18:41:59Z")

</div>

Hello everyone

I have this model

class DetailPurchase \< ActiveRecord::Base  
belongs\_to :purchase, :foreign\_key =\> ‘purchase\_id’  
belongs\_to :product, :foreign\_key =\> ‘product\_id’  
belongs\_to :buy\_order\_detail, :foreign\_key =\> ‘buy\_detail\_id’

def before\_create  
Storage.create!(:product\_id =\> self.product\_id, :current\_quantity  
=\> self.quantity, :stg\_data =\> purchase.prc\_data)  
end  
end

as you can see I’m using “before\_create” callback. It was working just  
fine two weeks ago, but today I realize it wasn’t. I don’t know why.  
The only change is that now I’m using jquery instead of prototype

---

<div class="post-metadata">

**Author:** ![resca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@resca](https://www.ruby-forum.com/u/resca)\
**Post date:** [August 16, 2011, 10:01pm UTC](https://www.ruby-forum.com/t/why-callback-does-not-work/210755/2 "2011-08-16T22:01:17Z")

</div>

On 16 August 2011 17:40, Angelo C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> Storage.create!(:product\_id =\> self.product\_id, :current\_quantity  
> =\> self.quantity, :stg\_data =\> purchase.prc\_data)

Use ruby-debug to break in here and inspect the data to see what is  
going on. See the Rails Guide on debugging if you don’t know how to  
do this.

> end  
> end
> 
> as you can see I’m using “before\_create” callback. It was working just  
> fine two weeks ago, but today I realize it wasn’t. I don’t know why.  
> The only change is that now I’m using jquery instead of prototype

You have not actually said what is not working. Have you looked in  
the rails log to see if there is anything of interest there?

Colin

---

<div class="post-metadata">

**Author:** ![resca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@resca](https://www.ruby-forum.com/u/resca)\
**Post date:** [August 16, 2011, 10:40pm UTC](https://www.ruby-forum.com/t/why-callback-does-not-work/210755/3 "2011-08-16T22:40:44Z")

</div>

The callback is not working. When I call to “Storage.create!” it  
should create a new Storage, but now the storage is never create.

Thanks for your help, I’ll try debugging.

---

<div class="post-metadata">

**Author:** ![resca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@resca](https://www.ruby-forum.com/u/resca)\
**Post date:** [August 16, 2011, 10:49pm UTC](https://www.ruby-forum.com/t/why-callback-does-not-work/210755/4 "2011-08-16T22:49:22Z")

</div>

On 16 August 2011 21:39, Angelo C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> The callback is not working. When I call to “Storage.create!” it  
> should create a new Storage, but now the storage is never create.
> 
> Thanks for your help, I’ll try debugging.

Debugging will tell you whether the callback is being called. If you  
assign the result of Storage.create! to a variable and break after  
that then you will be able to inspect that object.

Have you got any validations on Storage that could prevent it from  
saving?

Please don’t top post, it makes it difficult to follow the thread.  
Insert your reply at appropriate points in the previous message.  
Thanks.

Colin

---

<div class="post-metadata">

**Author:** ![resca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@resca](https://www.ruby-forum.com/u/resca)\
**Post date:** [August 16, 2011, 11:07pm UTC](https://www.ruby-forum.com/t/why-callback-does-not-work/210755/5 "2011-08-16T23:07:15Z")

</div>

On 16 August 2011 21:50, Tom M. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)  
wrote:

> you missed definition of that callback
> 
> class MyModel \< ActiveRecord::Base  
> before\_create :do\_my\_action

That can’t be the problem because it used to work and the only change  
the OP made was to use jQuery instead of prototype 🙂

@OP: have a look at your Source Control System logs (I presume you use  
git or something similar) to see what you actually changed.

Colin

---

<div class="post-metadata">

**Author:** ![resca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@resca](https://www.ruby-forum.com/u/resca)\
**Post date:** [August 17, 2011, 4:33pm UTC](https://www.ruby-forum.com/t/why-callback-does-not-work/210755/6 "2011-08-17T16:33:22Z")

</div>

> Debugging will tell you whether the callback is being called. If you  
> assign the result of Storage.create! to a variable and break after  
> that then you will be able to inspect that object.

I tried this

def before\_create  
debugger  
stg = Storage.create!(:product\_id =\>  
self.product\_id, :current\_quantity  
=\> self.quantity, :stg\_data =\> purchase.prc\_data)  
end

and when I “inspect” the variable “stg” I got “stg =\> nil” as result

---

<div class="post-metadata">

**Author:** ![resca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@resca](https://www.ruby-forum.com/u/resca)\
**Post date:** [August 18, 2011, 9:15am UTC](https://www.ruby-forum.com/t/why-callback-does-not-work/210755/7 "2011-08-18T09:15:21Z")

</div>

On 17 August 2011 15:31, Angelo C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> self.product\_id, :current\_quantity  
> =\> self.quantity, :stg\_data =\> purchase.prc\_data)  
> end
> 
> and when I “inspect” the variable “stg” I got “stg =\> nil” as result

That is not surprising since you have broken _before_ the line that  
allocates stg.

Colin

---

<div class="post-metadata">

**Author:** ![resca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@resca](https://www.ruby-forum.com/u/resca)\
**Post date:** [August 18, 2011, 3:51pm UTC](https://www.ruby-forum.com/t/why-callback-does-not-work/210755/8 "2011-08-18T15:51:55Z")

</div>

> That is not surprising since you have broken _before_ the line that  
> allocates stg.
> 
> Colin

If I break after that line… “stg” does not appear

---

<div class="post-metadata">

**Author:** ![resca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@resca](https://www.ruby-forum.com/u/resca)\
**Post date:** [August 16, 2011, 10:53pm UTC](https://www.ruby-forum.com/t/why-callback-does-not-work/210755/9 "2011-08-16T22:53:42Z")

</div>

you missed definition of that callback

class MyModel \< ActiveRecord::Base  
before\_create :do\_my\_action

def do\_my\_action  
.  
end  
end

tom

On Aug 16, 2011, at 22:39 , Angelo C. wrote:

> > > I have this model  
> > > Use ruby-debug to break in here and inspect the data to see what is  
> > > You have not actually said what is not working. Have you looked in  
> > > the rails log to see if there is anything of interest there?
> > 
> > Colin
> 
> –  
> You received this message because you are subscribed to the Google G. “Ruby  
> on Rails: Talk” group.  
> To post to this group, send email to [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> To unsubscribe from this group, send email to  
> [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid).  
> For more options, visit this group at  
> [http://groups.google.com/group/rubyonrails-talk?hl=en](http://groups.google.com/group/rubyonrails-talk?hl=en).

# –

Tomas Meinlschmidt, MS {MCT, MCP+I, MCSE, AER}, NetApp Filer/NetCache

# [www.meinlschmidt.com](http://www.meinlschmidt.com) www.maxwellrender.cz www.lightgems.cz

---

<div class="post-metadata">

**Author:** ![resca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@resca](https://www.ruby-forum.com/u/resca)\
**Post date:** [August 18, 2011, 6:49pm UTC](https://www.ruby-forum.com/t/why-callback-does-not-work/210755/10 "2011-08-18T18:49:42Z")

</div>

> Colin

When I used debugger the first time (in the line before the callback)  
I chose option “var local” and I got

stg =\> nil

But when I use debugger after that line I get this

blk =\> #\<Proc:0xc302b34@/usr/local/ruby/lib/ruby/gems/1.9.1/gems/  
activerecord-3.0.9/lib/active\_record/callbacks.rb:277\>  
halted =\> false  
key =\> nil  
name =\> nil  
result =\> true  
value =\> 88

And if I try to “inspect” stg I get

var instance stg  
NameError Exception: undefined local variable or method `stg’ for  
#DetailPurchase:0xb4d3054

---

<div class="post-metadata">

**Author:** ![resca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@resca](https://www.ruby-forum.com/u/resca)\
**Post date:** [August 18, 2011, 5:35pm UTC](https://www.ruby-forum.com/t/why-callback-does-not-work/210755/11 "2011-08-18T17:35:20Z")

</div>

On 18 August 2011 14:50, Angelo C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > That is not surprising since you have broken _before_ the line that  
> > allocates stg.
> > 
> > Colin
> 
> If I break after that line… “stg” does not appear

what do you mean it does not appear?

Remember that you can inspect data and evaluate expressions in the  
debugger.

Colin

---

<div class="post-metadata">

**Author:** ![resca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@resca](https://www.ruby-forum.com/u/resca)\
**Post date:** [August 18, 2011, 7:20pm UTC](https://www.ruby-forum.com/t/why-callback-does-not-work/210755/12 "2011-08-18T19:20:41Z")

</div>

On 18 August 2011 17:48, Angelo C. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid) wrote:

> > > > > and when I “inspect” the variable “stg” I got “stg =\> nil” as result  
> > > > > Remember that you can inspect data and evaluate expressions in the debugger.  
> > > > > blk =\> #\<Proc:0xc302b34@/usr/local/ruby/lib/ruby/gems/1.9.1/gems/  
> > > > > NameError Exception: undefined local variable or method `stg’ for  
> > > > > #DetailPurchase:0xb4d3054

I have occasionally had problems with the debugger statement at the  
end of a method with it apparently dropping out of the method before  
breaking, so that variables are no longer accessible. Add an extra  
line after the debugger statement (i=0 for example) to give it  
something to break on.

On a separate issue, looking at the message you get, are you using  
ruby 1.9.1? If so then upgrade to 1.9.2 or go back to 1.8.7, ruby  
1.9.1 does not work reliably with rails. This may have nothing to do  
with your problem.

I note that you still have not answered my question (unless I missed  
it), have you any validations on the Storage model?

Colin

---

<div class="post-metadata">

**Author:** ![resca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@resca](https://www.ruby-forum.com/u/resca)\
**Post date:** [August 18, 2011, 9:38pm UTC](https://www.ruby-forum.com/t/why-callback-does-not-work/210755/13 "2011-08-18T21:38:48Z")

</div>

> On a separate issue, looking at the message you get, are you using  
> ruby 1.9.1? If so then upgrade to 1.9.2 or go back to 1.8.7, ruby  
> 1.9.1 does not work reliably with rails. This may have nothing to do  
> with your problem.

No, I’m using ruby 1.9.2

> I note that you still have not answered my question (unless I missed  
> it), have you any validations on the Storage model?

Finally, I found the problem. It was a validation on storage, I  
removed it, and everything works perfect

Thanks for your help, thanks for your patience

---

<div class="post-metadata">

**Author:** ![resca](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/r/bbe5ce/32.png) [@resca](https://www.ruby-forum.com/u/resca)\
**Post date:** [August 18, 2011, 9:27pm UTC](https://www.ruby-forum.com/t/why-callback-does-not-work/210755/14 "2011-08-18T21:27:40Z")

</div>

> On a separate issue, looking at the message you get, are you using  
> ruby 1.9.1? If so then upgrade to 1.9.2 or go back to 1.8.7, ruby  
> 1.9.1 does not work reliably with rails. This may have nothing to do  
> with your problem.

I’m using ruby 1.9.2

> I note that you still have not answered my question (unless I missed  
> it), have you any validations on the Storage model?

I have validations for presence (product\_id, current\_quantity and  
stg\_data)  
and for numericality (current\_quantity)
