ruby on rails - Duplicate error messages with validates_associated -


here 2 classes, 1:n relation

class company < ar::base   has_many :brands   validates_associated :brands end  class brand < ar::base   belongs_to :company   validates_presence_of :name end 

i try add brands company. if brand name empty, gives me duplicated error messages.

c = company.find(1) c.valid? # => true c.brands.new # => #<brand id: nil, name: nil, company_id: 1, created_at: nil, updated_at: nil> c.valid? #=> false c.errors.full_message #=> ["brands invalid", "brands invalid"] c.brands.last.errors.full_message #=> ["name required"] 

validates associated can achieved 2 ways
first option simple:

has_many :brands, validate: true 

second option using validates_associated cause duplicate error message , can avoided explicitly setting validate false:

has_many :brands, validate: false validates_associated :brands 

note:
can go second option if need additional options validates_associated :if, :unless etc... otherwise go first option not cause duplicate errors.


Comments

Popular posts from this blog

basic authentication with http post params android -

vb.net - Virtual Keyboard commands -

css - Firefox for ubuntu renders wrong colors -