我接受了您在setup方法中首先删除重要属性的想法,但使用了alias\u chain\u方法,使其更透明:
def attributes_with_set_important_attribute_first=(attributes = {})
# Make sure not to accidentally blank out the important_attribute when none is passed in
if attributes.symbolize_keys!.include?(:important_attribute)
self.important_attribute = attributes.delete(:important_attribute)
end
self.attributes_without_set_important_attribute_first = attributes
end
alias_method_chain :attributes=, :set_important_attribute_first
这样,您的代码就不会改变正常的Rails样式
@other_model = @some_model.other_models.build(params[:other_model])