In my model I have specified a before_save method to run, 开发者_开发知识库and check the new record against some data. If the new record isn't what I want - how can I stop it from being saved?
Here is essentially what I'm trying to do (and failing):
before_save :itemCheck
def itemCheck
if self.item_type_id == 1
if self.num > 6
self.destroy
end
end
end
NOTE: my code is more complicated than this - just making a simple example.
Return false
from your before_save
and the record won't be saved.
As a sidenote: don't use camelcase for functions, but use: item_check
.
精彩评论