这是我的Lesson
模型:
before_create :set_sequence
def set_sequence
maxseq = Lesson.where(:course_id => self.course_id).maximum("sequence")
if (maxseq.nil?)
maxseq = 0
end
self.sequence = maxseq + 1
end
当我运行rspec
以下测试失败时:
it "validate sequence is setup" do
lesson = Lesson.create(:title => "Testing", :description => "Testing", :course_id => 1)
lesson.sequence.should_not eql nil
end
但是,当通过对象进行 T 测试时,rails console
该Lesson
对象已成功创建并具有正确的顺序。任何想法为什么?