Uploaded image for project: 'Mongoid'
  1. Mongoid
  2. MONGOID-3650

Building with scopes from relations don't maintain relationship

    • Type: Icon: Task Task
    • Resolution: Done
    • Priority: Icon: Major - P3 Major - P3
    • 5.0.1
    • Affects Version/s: None
    • Component/s: None
    • None

      Using: 4.0.0.beta1

      Given:

      class School
        include Mongoid::Document
        
        has_many :students
      end
      
      class Student
        include Mongoid::Document
      
        belongs_to :school
      
        scope :enrolled, -> { where(state: 'enrolled') }
      end
      
      school = School.new
      student = school.students.build()
      student.school == school # => true
      

      Then if I use a scope, say enrolled, I might expect similar behavior, except I get:

      school = School.new
      student = school.students.enrolled.build()
      student.school == school # => false
      student.school.nil? # => true
      student.state # => "enrolled", expected behavior for this scope
      

      So the parent of the relation doesn't stay attached to the new child when using a scope.

            Assignee:
            dianna.hohensee@mongodb.com Dianna Hohensee (Inactive)
            Reporter:
            kylemacey kylemacey
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: