大概故事是这样……
class Company < AR::Base
has_many :brands
validates_associated :brands
end
class Brand < AR::Base
belongs_to :company
validates_presence_of :name
end
然后前台更新 Company 的时候,如果添加一个 brand 但是不填名字,会给出两条出错的信息。
BTW,如果添加两个 brand 且不填名字。会给出 3 条……
我进 rails console 之后去试验,也会有同样的问题
c = Company.find(1)
c.valid?
=> true
c.brands.new
=> #<Brand id: nil, custom: nil, name: nil, company_id: 1, created_at: nil, updated_at: nil>
c.valid?
=> false
c.errors.full_message
=> ["Brands is invalid", "Brands is invalid"]
c.brands.last.errors.full_message
=> ["Name required"]