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

Many to Many relation with a helper model, destroying helper object doesn't remove it from Model's relateds

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

      In my case, I have a User model that has many Projects and a Project that has many Users, and the relationship between User and Project has attributes such as rate,progress and etc.

      Unable to find source-code formatter for language: `ruby. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      class Project
        include Mongoid::Document
        field :title, type: String
        field :description, type: String
      
        has_many :related_users, class_name: 'ProjectMembership', inverse_of: :project
      end
      
      class User
        include Mongoid::Document
        field :firstname, type: String
        field :lastname, type: String
        has_many :related_projects, class_name: 'ProjectMembership', inverse_of: :user
      end
      
      class ProjectMembership
        include Mongoid::Document
        field :rate,type: Float
        belongs_to :user, class_name: 'Project', inverse_of: :related_projects
        belongs_to :project, class_name: 'User', inverse_of: :related_users
      end
      

      `

      When I try to remove ralated_project from user with this function :

      Unable to find source-code formatter for language: `ruby. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
        def remove_project(pid)
          rel = self.related_projects.where(project_id: pid)[0]
          if rel
            rel.destroy
          end
        end
      

      `

      It removes ProjectMembership object correctly but user.related_projects still has that ProjectMembership object :

      Unable to find source-code formatter for language: `ruby. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
        def get_projects
          ret = []
          self.related_projects.each do |rel|
            ret << Project.find(rel.project_id)
          end
          ret
        end
      

      `

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            KeyKaKiTO KeyKaKiTO [X]
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: