freelancing-god/ts-delayed-delta — Issue #19: Delta index runs even if object was not persisted
Author: enrico
Created: 2012-01-29 20:31:22+00:00
State: open

I just exposed some APIs to my app, and discovered this problem. When I invoke my API to create a new record, i.e.

```
business.customers.create(:first_name => 'foo', ...)
```

and there is a validation error (i.e. email is already taken). I get an exception on my log:

```
Started POST "/api/customers" for 127.0.0.1 at 2012-01-29 14:10:49 -0600
User Load (0.7ms) SELECT `users`.* FROM `users` WHERE `users`.`authentication_token` = 'vZSDZumq7doM4tgxoTdSHHXR3m1Pqs8C' AND `users`.`current_state` IS NULL LIMIT 1
Business Load (0.6ms) SELECT `businesses`.* FROM `businesses` WHERE `businesses`.`id` = 1 LIMIT 1
(0.1ms) BEGIN
(0.3ms) SELECT 1 FROM `customers` WHERE (LOWER(`customers`.`email`) = LOWER('foo@bar.com') AND `customers`.`business_id` = 1) LIMIT 1
(0.1ms) COMMIT
(0.4ms) SELECT COUNT(*) FROM `delayed_jobs` WHERE `delayed_jobs`.`handler` = '--- !ruby/object:ThinkingSphinx::Deltas::DeltaJob \nindices: \n- customer_delta\n' AND `delayed_jobs`.`locked_at` IS NULL
You have a nil object when you didn't expect it!
You might have expected an instance of Array.
The error occurred while evaluating nil.*

```

I believe this is due to the fact that delayed delta checks whether the delta column has been toggled(which in my case was true), but not whether the object has been persisted.

I wanted to provide a patch, but I'm not sure where to add the check: I found 2 places that look like they need it:

delta.rb: line 45

delayed_delta.rb: line 63-65

am I wrong? any feedback?