Everything was working as expected, until I upgraded to Rails 3.2.11.
This is how my models are setup :
class Student < ActiveRecord::Base
has_many :institutes
has_many :teachers, :through => :institutes
end
class Teacher < ActiveRecord::Base
has_many :institutes
has_many :students, :through => :institutes
end
class Institute < ActiveRecord::Base
belongs_to :teachers
belongs_to :students
validates :teacher_id, :presence => true
validates :student_id, :uniqueness => {:scope => :teacher_id}, :presence => true
end
My factories.rb file looks like this :
factory :student do
first_name "abc"
last_name "xyz"
teachers {|t| [t.association(:teacher)] }
end
factory :teacher do
first_name "ABC"
last_name "XYZ"
end
factory :institute do
association :student
association :teacher
end
The Problem :
When I do :
FactoryGirl.create(:student)
It gives me the following error :
ActiveRecord::RecordInvalid: Validation failed: Institutes is invalid
It seems like, it creates :teacher, then :institute and finally :student. Therefore, it doesn’t have the student_id when it creates :institute, making it invalid.
The weird thing is, the same model and factory_girl setup was working fine with Rails 3.2.8.
How can I fix this ?
What about somthing like this