rails模型做了一些破坏

时间:2015-04-25 03:00:31

标签: ruby-on-rails ruby-on-rails-4 activerecord

如何在模型被销毁之前调用模型中编写方法?

例如,当Model Adestroyed

时,我想写一个循环来更新另一个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'] 中的一堆内容

2 个答案:

答案 0 :(得分:2)

回调是什么,在您的情况下,您可以使用before_destroy回调,例如:

class ModelA
before_destroy :do_bunch_of_things

def do_bunch_of_things
...
end
end

答案 1 :(得分:1)

当一个对象被销毁时,Active Record有三个可用的回调:before_destroyaround_destroyafter_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