Why callback does not work

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

On 16 August 2011 17:40, Angelo C. [email protected] 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

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.

On 16 August 2011 21:39, Angelo C. [email protected] 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

On 16 August 2011 21:50, Tom M. [email protected]
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 :slight_smile:

@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

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

On 17 August 2011 15:31, Angelo C. [email protected] 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

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

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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en.

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

www.meinlschmidt.com www.maxwellrender.cz www.lightgems.cz

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:[email protected]/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

On 18 August 2011 14:50, Angelo C. [email protected] 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

On 18 August 2011 17:48, Angelo C. [email protected] 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:[email protected]/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

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

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)