Guardian、Student、Relationship、RelationshipType、Schoolの5つのモデルがあります。それらの間に私はこれらの団体を持っていますファクトリー内の2つの関連付けを取得して別の関連付けを行う
class Guardian < ActiveRecord::Base
belongs_to :school
has_many :relationships, :dependent => :destroy
has_many :students, :through => :relationships
end
class Student < ActiveRecord::Base
belongs_to :school
has_many :relationships, :dependent => :destroy
has_many :guardians, :through => :relationships
end
class Relationship < ActiveRecord::Base
belongs_to :student
belongs_to :guardian
belongs_to :relationship_type
end
class School < ActiveRecord::Base
has_many :guardians, :dependent => :destroy
has_many :students, :dependent => :destroy
end
class RelationshipType < ActiveRecord::Base
has_many :relationships
end
関係を定義するFactoryGirlを書いてみたいと思います。すべての関係には保護者と学生がいなければなりません。これら2つは同じ学校に属していなければなりません。保護者工場には学校との関連があり、学生工場も同様です。私はそれらを同じ学校に建設することができませんでした。に、
undefined method `school' for #<FactoryGirl::Declaration::Implicit:0x0000010098af98> (NoMethodError)
私が欲しいものを行うにはどのような方法があります:私はこのファクトリを使用して関係を構築しようとすると、これは、次のエラーが発生し
FactoryGirl.define do
factory :relationship do
association :guardian
association :student, :school => self.guardian.school
relationship_type RelationshipType.first
end
end
:私は、次のコードを持っています既に作成された保護者と生徒を工場に渡すことに頼らなくても、保護者と学生を同じ学校に所属させることができます(これは目的ではありません)。
これはエラーと関係がありますが、私の編集前の2番目のRelationshipクラス宣言としてSchoolクラスが書かれているかどうかはわかりません。 – PinnyM