On Wednesday, February 19, 2014 8:29:28 PM UTC-8, [email protected] 
wrote:
>
>  Hi there,
>
> Only after I opened an issue at Github I realized I should have posted 
> this here before, sorry about that...
>
> From Sequel documentation re: Model Hooks:
>
> So when a model object is created, you want to add a background job that 
> will create the thumbnail for the picture. If you used after_save for this 
> and transactions are being used, you are subject to a race condition where 
> the background job library will check the database table for the record 
> before the transaction that saved the record commits, and it won't be able 
> to see the record's data. Using after_commit, you are guaranteed that the 
> background job library will not get notified of the record until after the 
> transaction commits and the data is viewable.
>
> Unless I'm missing something, there is no built-in way to check if the 
> current record was new on an after_commit hook. I have a use case where I 
> need to perform a background job only for new records, so in the 
> after_save hook I'm setting my own flag based on @was_new.
>
> Here we can see that @was_new is reset just before the after_commit hook 
> runs:
>
> https://github.com/jeremyevans/sequel/blob/master/lib/sequel/model/base.rb#L1656-L1665
>
> Could we make @was_new available in the after_commit hook? Or is there 
> another to achieve this
>

@was_new cannot be made available in after_commit, because you never know 
when after_commit will run:

  DB.transaction do
    model = Model.create(:a=>1)
    model.update(:b=>2)
  end

@was_new is a bad idea, and it's my mistake for introducing it.  Anyway, 
you can do what you want via after_create:

  def after_create
    super
    db.after_commit{...}
  end

Were I to do it over again, there wouldn't be model commit/rollback hooks, 
you would have to call Database#after_commit/after_rollback in the other 
model hooks.

Thanks,
Jeremy

-- 
You received this message because you are subscribed to the Google Groups 
"sequel-talk" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/sequel-talk.
For more options, visit https://groups.google.com/groups/opt_out.

Reply via email to