If you want to use this approach and make sure
destroy_author is called, you need to update your Reply model to take advantage of the super
command:
CHAPTER 4 ?– CORE FEATURES OF ACTIVE RECORD 60
class Reply < Topic
def before_destroy()
destroy_readers
super.destroy_author
end
end
Callback Macros
Of course, there??™s an easier way to that ensure your callbacks are kept intact down through the
inheritance hierarchy??”callback macros. Continuing with our example from the Active Record
documentation, you could rewrite the example using macros:
class Topic < ActiveRecord::Base
before_destroy :destroy_author
end
class Reply < Topic
before_destroy :destroy_readers
end
Now, when you delete a reply, both the destroy_readers and destroy_author methods are
called.
?– Note If you intend to use callback macros to ensure your callbacks are kept intact down through your
inheritance hierarchy, it??™s important that you define your callback macros in your model beforeyou specify
your associations. Otherwise, you risk loading your children records before their parent callback, and therefore,
the parent callback would not be applied to the children.
Pages:
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173