Created
May 9, 2013 03:46
-
-
Save tehprofessor/5545416 to your computer and use it in GitHub Desktop.
How to determine if an instance was created (e.g. not previously persisted) after it has been saved. Very much use case specific.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Model < ActiveRecord::Base | |
attr_accessor :was_new_record | |
def was_new_record? | |
@was_new_record == true ? true : false | |
end | |
end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ModelObserver < ActiveRecord::Observer | |
def before_create(model) | |
model.was_new_record = true | |
end | |
def after_commit(model) | |
model.do_stuff if model.was_new_record? | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment