Bholzer
Bholzer

Reputation: 189

Why is my Rails callback being called twice?

I have an after_commit on: :create callback in my model, and it is being called twice. There are six methods in this callback, some of which are inserting into my database. I am afraid things are going to slow down AND my database is going to grow too quickly.

What's odd is that my before_create after_create callbacks are only executing once. What could be causing this?

Upvotes: 6

Views: 6392

Answers (2)

I found using before_create solved the issue.

When you use the:

user = User.new
user.save

You are firing the before_save hook each time.

before_create

should work

Upvotes: 1

house9
house9

Reputation: 20594

Short answer: use after_save instead of after_commit

Long answer: How to organize complex callbacks in Rails?

Upvotes: 6

Related Questions