mardi 4 août 2015

Rails validation, knowing which field is invalid?

i had a model which works fine.

class Product < ActiveRecord::Base
  validates_format_of :field1, with: /\A[0-9\+\-\/\s]+\Z/, allow_nil: true
  validates_format_of :field2, with: /\A[0-9\+\-\/\s]+\Z/, allow_nil: true
end

some of my entries in the database are invalid. these are older entries. i want to run a seed which find these invalid entries

Product.all.each do |product|
  next if product.valid?
  # 
end

i want to clear the attributes which are invalid. let me say, Product 1 has the value test in field1 which is invalid. now i want to clear this field, and only this.

how do i find the fields which are invalid? something like product.field1.valid?

Aucun commentaire:

Enregistrer un commentaire