1

我有以下两个模型:

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 需要特定的 validate: 回调,而 validates 方法不够/不可接受。如何告诉 has_many 关联运行验证回调,但不需要 :validate => ?

4

0 回答 0