2014-01-08 54 views

回答

6

导轨有许多不同的callbacks。当你创建一个对象after_createafter_save被调用。更新现有对象时,调用after_updateafter_save

after_create仅在创建时使用,因为after_update仅用于更新现有对象。在正确的创建/更新回调之后,after_save都会在两种情况下运行。

如果您不想仅在创建对象后才调用该方法,则应该使用after_create代替。

,因为你需要

class User < ActiveRecord::Base 
    after_create :one_method 
    after_create :another_method 
    after_update :one_more_method_only_for_updates 

    def one_method 
    end 

    def another_method 
    end 

    def one_more_method_only_for_updates 
    end 
end 
+0

我的问题很可能是不够清楚,您可以定义尽可能多的回调。我有一个方法,我只想在创建对象时调用,另一种方法是在对象更新但不创建时调用。 – Albert

+1

你可以有很多回调。我已更新示例以显示 – wacko

+0

因此,当我正确理解您时,您说在创建对象后,after_update回调不会被调用。但是,就我而言,它似乎确实被调用。 – Albert