Frexuz
Frexuz

Reputation: 4933

Sidekiq processing in after_create fetches wrong number of records?

For some reason, Sidekiq gives me back 0 records for a query, after being triggered in an after_create. However, if process would have sleep 1 in it, it ALWAYS gives me the correct value (1 record).
What the hell is going on here?

Model:

after_create :after_create

def after_create
    AwardsWorker.perform_async(user_id)
end

Worker:

def perform(user_id)
  user = User.find(user_id)
  AwardEngine.added_shows_awards(user)
end

AwardEngine:

def self.added_shows_awards(user)
  count = user.followings(true).count #(true) to avoid AR caching
  puts "count: #{count}" #this is mostly 0! even if the database has 1 record
end

Upvotes: 1

Views: 870

Answers (1)

Mike Perham
Mike Perham

Reputation: 22218

You need to use after_commit, as explained in the wiki:

https://github.com/mperham/sidekiq/wiki/Problems-and-Troubleshooting#cannot-find-modelname-with-id12345

Upvotes: 4

Related Questions