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

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)

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.

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

you missed definition of that callback

class MyModel < ActiveRecord::Base   before_create :do_my_action

   def do_my_action    ….   end end

tom

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

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

Colin

> 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

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

>> > 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

>> 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

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>

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