对Rails的嵌套属性进行has_many验证

时间:2013-03-18 19:41:00

标签: ruby ruby-on-rails-3 rails-activerecord shoulda

我有以下两种模式:

class Human < ActiveRecord::Base

 has_many :homes, inverse_of: :human, validate: true, autosave: true
 accepts_nested_attributes_for :homes, allow_destroy: true
end


class Home < ActiveRecord::Base
 belongs_to :human, inverse_of: :homes

 validates :human, presence: true
 validates :address, presence: true, length: { maximum: 255 },
        uniqueness: { case_sensitive: false, scope: :human_id }
 validates :post_code, length: { maximum: 25 }
end

以下spec/models/human_spec.rb

的规范
it { should have_many(:homes) }

哪个失败了:

 Failure/Error: it { should have_many(:homes) }
   Expected Human to have a has_many association called homes (homes should have :validate => )
 # ./spec/models/human_spec.rb:53:in `block (3 levels) in <top (required)>'

我已将此跟踪到此处:

  https://github.com/thoughtbot/shoulda-matchers/blob/master/lib/shoulda/matchers/active_record/association_matcher.rb  (Line 263)

这似乎表明validate:true需要特定的验证:回调,验证方法不充分/可接受。如何告诉has_many关联运行验证回调,但不要求:validate =&gt; ?

0 个答案:

没有答案