ruby on rails - Check object's errors on model.save -


let's have following validation run on model.save action:

  def max_count     errors.add(:base, 'cannot save more 5 records') if self.class.active.count == 5   end 

why model.errors object nil upon save?

this post can used reference how check database state before saving new records .

if use binding.pry should first run

object.valid? # load it's errors, if 

and can see it's errors with

object.errors 

firstly, seed test database 5 is_active objects, write test:

it 'has error when creating sixth object'   obj = model.new(name: 'name', is_active: true)   obj.valid?   expect(obj.errors[:base]).to eq 'cannot save more 5 records' end 

Comments