我想我知道问题出在哪里,但我似乎无法弄清楚如何解决它。
这是我的models
用户
class User < ActiveRecord::Base
attr_accessor :password
attr_accessible :name, :email, :password, :password_confirmation
has_many :student_groups
...
end
学生组
class StudentGroup < ActiveRecord::Base
attr_accessible :name
belongs_to :user
has_many :subjects
has_many :students
end
主题
class Subject < ActiveRecord::Base
attr_accessible :end_date, :name
belongs_to :student_group
belongs_to :student
end
学生
class Student < ActiveRecord::Base
attr_accessible :gender, :name
belongs_to :student_group
has_many :subjects
end
在我的Student_Spec.rb
我有以下测试编辑:
...
before(:each) do
@user = Factory(:user)
@student_group_attr = { name: "4a"}
@student_group = @user.student_groups.create(@student_group_attr)
@date = Date.today+180
@subject_attr = { name: "English", end_date: @date}
end
...
describe "Student associations" do
before(:each) do
@subject = @student_group.subjects.create!(@subject_attr)
@student_attr = { name: "Example Student", gender: "Female" }
@student = @student_group.students.create(@student_attr)
end
it "should have the right associated student" do
@subject.student_id.should == @student.id
@subject.student.should == @student
end
end
我在其他规格中进行了相同的测试并且效果很好 - 我在控制台中检查了它并得到了这个:
2.0.0-p0 :015 > @subject
=> #<Subject id: 1, name: "English", student_group_id: 1, student_id: nil, end_date: "2013-11-18", created_at: "2013-05-22 15:08:44", updated_at: "2013-05-22 15:08:44">
因此,无论出于何种原因,student_id 都没有与主题相关联……我在这里做错了什么?谢谢!