[Backgroundrb-devel] Very strange after_save problem. Please help.
ezmobius at gmail.com
Mon Jul 17 19:14:50 EDT 2006
On Jul 17, 2006, at 3:49 PM, Ben Johnson wrote:
> I have a very strange problem here. I do not get this. So any help
> is greatly appreciated.
> Basically I have a model that calls a method in the background
> process in the "after_save" method. Let's call the model products.
> So what happens is this:
> 1. I create a new product.
> 2. Everything works and the product is saved with id 13.
> 3. A method is called in my background process passing it the NEW
> product id in the after_save method.
> 4. That method in the background process says "Can't find Product
> with id=13".
> 5. An exception is then passed back to my after_save method and the
> product rolls back like adding it never happened. Because when an
> exception is raised in after_save it rolls back.
> So I figured maybe it wasn't connecting to the database or
> something. I adding a product in the db manually and added the
> following in my backgroundrb method:
> p = Product.find_all
> raise p.inspect
> I try to add another product and it shows me that product I just
> added into the database manually but not the one that was just
> created through the product model. So the backgrond process is
> connecting to the database.
> So in my after_save method i did the following to make sure it was
> getting into the database:
> p = Product.find id
> raise p.inspect
> I tried to add a product again and it raised an exception with the
> product information that I JUST added.
> Why is backgroundrb not seeing new additions to the database when
> being called from after_save? It's like backgroundrb doesn't
> acknowledge it's existence until after_save is finished.
> Anyways, if this is the case, is there a work around so that I can
> invoke a backgroundrb method on a freshly saved product?
> Thanks a lot for your help.
> Thank You,
> Ben Johnson
> E: bjohnson at contuitive.com
I'm not 100% certain but what I think is happening is that since the
after_save is run inside the transaction for that save, the new item
is not commited to the db because the after_save is failing. Now i
think whats happening is that since the save has not been commited in
rails yet, it isn't actually in the db yet so your background worker
cannot see it yet.
I think that instead of trying to do this in after_save that maybe
you should put the call to your background worker in an AR Observer
that runs the background task *after* the save has been commited.
Basically I think you are short circuiting yourself becazsue the
record is not saved to the db until the transaction is commited but
your drb task cannot find that record until after it is commited.
Make any sense?
-------------- next part --------------
An HTML attachment was scrubbed...
More information about the Backgroundrb-devel