开发者

Destroy a record before it's saved in rails 3

开发者 https://www.devze.com 2023-04-07 00:52 出处:网络
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?

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.

0

精彩评论

暂无评论...
验证码 换一张
取 消