1
経由にhas_manyを検証し、私は、文書間の多くの関係に多くを持っています。RSpecのテストは非関係
は、私がdocument1
とdocument2
を持っていると言います。私は両親と子供がいるところに多人数のテーブルを持っています。
belongs_to :parent, :class_name => "Document", :foreign_key => "parent_id"
belongs_to :child, :class_name => "Document", :foreign_key => "child_id"
validates_uniqueness_of :child_id, :scope => [:parent_id]
validates_presence_of :parent_id
validates_presence_of :child_id
validate :obeys_chronology
def obeys_chronology
errors.add(:child_id, "must be created after its parent") if child_id.to_i < parent_id.to_i
errors.add(:child_id, "cannot be its own parent") if child_id.to_i == parent_id.to_i
end
document_relationship.rb
document.rb
has_many :child_relationships, :class_name => "DocumentRelationship", :foreign_key => "child_id", :dependent => :destroy
has_many :parents, :through => :child_relationships, :source => :parent
has_many :parent_relationships, :class_name => "DocumentRelationship", :foreign_key => "parent_id", :dependent => :destroy
has_many :children, :through => :parent_relationships, :source => :child
私はdocument2.children << document1
を言うなら、それは適切な検証が失敗したが、私は、このためのテストを書く方法がわかりません。
これを行うより良い方法はありますか?
私はそれを試しました。 失敗/エラー:d2.children << d1 ActiveRecord :: RecordInvalid: 検証に失敗しました:子は親の後に作成する必要があります #./spec/models/document_spec.rb:65:in 'block(3 levels)in ' –
Cyrus