
[英]Ruby on Rails: What is the opposite of the build method? Or, can I destroy an object from memory before it's saved?
[英]Destroy a record before it's saved in rails 3
在我的模型中,我指定了一个要运行的before_save方法,并根据某些数据检查新记录。 如果新记录不是我想要的 - 我怎么能阻止它被保存?
这基本上是我想要做的(并且失败):
before_save :itemCheck
def itemCheck
if self.item_type_id == 1
if self.num > 6
self.destroy
end
end
end
注意:我的代码比这更复杂 - 只是做一个简单的例子。
从before_save
返回false
并且不会保存记录。
作为旁注:不要将camelcase用于函数,而是使用: item_check
。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.