0
  • The use case is we are having a lms_status model & in the model having after_create callback with if condition.

  • I found that when if conditions is true then the code inside the after_create executed properly but in the lms_statuses table no entry created.

  • It seems that after created it also deleted from table.

    class LmsStatus
    
     after_create :enqueue_order_item, if: ->{ status == 6 }
    
     def enqueue_order_item
      //code working fine
     end   
    
    end
    
  • It is happening for some of the lms_status object/entry not always.

  • Is it possible that after_create callback after executing the code inside callback can rollback ? If Yes then for those lms_statuses, it is reverting I do not want to execute the callback code. How can I prevent ?

  • I am not really sure if I understand your problem and what you try to achieve. But the creation of the record and the after create callback run in the same outer transaction. Each step in that transaction can trigger a rollback. If such a rollback is triggered, then the creation but also the modification done by that callback will be reverted. Does that help? Or can you elaborate on the problem you are facing? – spickermann Nov 26 '22 at 16:43
  • the problem is that after_create code should be executed after creation of a lms_status object but in my case after_create code has executed but I did not find any lms_status for that. For Example suppose after_create code executed for #lms_status_1 but in the table no entry #lms_status_1...@spickermann – Anand Shrivastava Nov 27 '22 at 08:25
  • 2
    By using `if: 'status==6'` you're making it so that the callback will always run since everything except `nil` and `false` are truthy in Ruby. Rails doesn't eval the string AFAIK. You want to use a lamda `if: ->{ status == 6 }` or better yet define an enum so you can call something like `if: :draft?`. https://guides.rubyonrails.org/active_record_callbacks.html#conditional-callbacks – max Nov 27 '22 at 08:30
  • I updated the if condition....It was just for example...sorry the syntax was not correct... the issue is something else @max – Anand Shrivastava Nov 27 '22 at 08:37
  • 2
    I don't think anyone can actually help you without an example that demonstrates the entire problem that you're facing. And examples of what happens vs what the desired outcome is. – max Nov 27 '22 at 08:43

0 Answers0