ActiveRecord before_update 回调理解
ActiveRecord before_update callback understanding
我理解正确吗,如果我执行
model = Model.create(some_attr: "attr_value")
model.update(some_attr: "new_attr_value")
对于模型我有
before_update :before_update_callback
def before_update_callback
some_attr
end
该回调将 return“new_attr_value”,因为内部 ruby 对象(变量“model”)在调用回调之前已更改。
是的。这正是将要发生的事情。
# Updates the attributes of the model from the passed-in hash and saves the
# record, all wrapped in a transaction. If the object is invalid, the saving
# will fail and false will be returned.
def update(attributes)
# The following transaction covers any possible database side-effects of the
# attributes assignment. For example, setting the IDs of a child collection.
with_transaction_returning_status do
assign_attributes(attributes)
save
end
end
参见:
我理解正确吗,如果我执行
model = Model.create(some_attr: "attr_value")
model.update(some_attr: "new_attr_value")
对于模型我有
before_update :before_update_callback
def before_update_callback
some_attr
end
该回调将 return“new_attr_value”,因为内部 ruby 对象(变量“model”)在调用回调之前已更改。
是的。这正是将要发生的事情。
# Updates the attributes of the model from the passed-in hash and saves the
# record, all wrapped in a transaction. If the object is invalid, the saving
# will fail and false will be returned.
def update(attributes)
# The following transaction covers any possible database side-effects of the
# attributes assignment. For example, setting the IDs of a child collection.
with_transaction_returning_status do
assign_attributes(attributes)
save
end
end
参见: