Elliot
Elliot

Reputation: 13835

Destroy a record before it's saved in rails 3

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.

Upvotes: 1

Views: 2058

Answers (1)

rdvdijk
rdvdijk

Reputation: 4398

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.

Upvotes: 5

Related Questions