如何在模型被销毁之前调用模型中编写方法?
例如,当Model A
为destroyed
String insertText = "INSERT INTO alpha_screen (alpha, screen) VALUES (?,?)";
PreparedStatement preparedStatement = InsertWordCount.PrepareText(insertText);
BoundStatement boundStatement = preparedStatement.bind(keyvalue, newword); //INSERT positions
getSession().execute(boundStatement);
cqlsh> SELECT * FROM rant.alpha_screen; ==> The INSERTS are done as expected.
alpha | screen
-------+--------------
a | ['aboard']
c | ['checking']
p | ['pull']
r | ['rotting']
t | ['time']
中的一堆内容
答案 0 :(得分:2)
回调是什么,在您的情况下,您可以使用before_destroy回调,例如:
class ModelA
before_destroy :do_bunch_of_things
def do_bunch_of_things
...
end
end
答案 1 :(得分:1)
当一个对象被销毁时,Active Record有三个可用的回调:before_destroy
,around_destroy
和after_destroy
。一个例子:
class Article < ActiveRecord::Base
after_destroy :log_destroy_action
def log_destroy_action
puts 'Article destroyed'
end
end
此处提供更多信息:http://guides.rubyonrails.org/active_record_callbacks.html