Guest User

Untitled

a guest
May 24th, 2018
150
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.13 KB | None | 0 0
  1.  
  2. ##the assertion fails saying the length is zero
  3. def test_position
  4. subject_one =courses(:one).course_subjects.create!(:position => courses(:one).subjects.length)
  5. subject_two =courses(:one).course_subjects.create!(:position => courses(:one).subjects.length)
  6. subject_three =courses(:one).course_subjects.create!(:position => courses(:one).subjects.length)
  7. subject_one.subject = subjects(:one)
  8. subject_two.subject = subjects(:two)
  9. subject_three.subject = subjects(:three)
  10. subject_one.subject.save
  11. assert_equal courses(:one).subjects.length, 3
  12. end
  13. ##a course
  14. class Course < ActiveRecord::Base
  15. belongs_to :author, :class_name=>'User'
  16. has_many :course_subjects, :order => :position, :before_add => Proc.new {| course, subject| subject.position = course.subjects.length}
  17. has_many :subjects, :through => :course_subjects
  18. end
  19. ##a course subject
  20. class CourseSubject < ActiveRecord::Base
  21. belongs_to :course
  22. belongs_to :subject
  23. end
  24. ##a subject
  25. class Subject < ActiveRecord::Base
  26. belongs_to :subject
  27. has_many :course_subjects, :order => :position
  28. has_many :courses, :through => :course_subjects
  29. end
Add Comment
Please, Sign In to add comment