The Whiz of Oz
The Whiz of Oz

Reputation: 7043

Skip after_save callback on create

I have an after_save callback in my model:

after_save :do_stuff

However I want Rails to ignore it on my model's create action, something like

after_save :do_stuff, skip: on_create

What's a good way to do that?

Upvotes: 0

Views: 1121

Answers (3)

lg86
lg86

Reputation: 375

You can use except try these:

after_save :do_stuff, except: :create

Or

use after_update

Upvotes: 2

Ziv Galili
Ziv Galili

Reputation: 1445

In that case - don't use after_save callback.

The purpose of after_save is to run every time you SAVE an object, so it will get called when the object is created (-> because it get saved for the first time) and when the object is being updated.

As @JesusTinoco answered, you should use the after_update callback which will get called only when you UPDATE the object.

after_update :do_stuff

(PS: there is also after_create callback which will get called only when you CREATE an object.)

Upvotes: 0

JesusTinoco
JesusTinoco

Reputation: 11828

You can use the after_update callback. As the documentation said:

Is called after Base.save on existing objects that have a record.

Upvotes: 1

Related Questions