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

embeded custom relation names bug

    • Type: Icon: Task Task
    • Resolution: Done
    • 2.4.11
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      basically, if you want to use a belongs_to association with the same name as the embedded association's class, things break

      so, if a company has many customers, and each customer has a company where she works, then I can't embed the association with the correct names...

      require 'mongoid'
      require 'pp'
      
      ENV['RACK_ENV'] = 'development'
      Mongoid.load!("mongoid.yml")
      
      class Company
        include Mongoid::Document
        field :name, type: String
        embeds_many :customers
      end
      
      class Customer
        include Mongoid::Document
        embedded_in :vendor, class_name: 'Company', inverse_of: :customers
        belongs_to :company, inverse_of: nil
      end
      
      Company.all.delete
      
      a = Company.create! name: 'A'
      b = Company.create! name: 'B'
      
      a.customers.create! company: b
      
      customer = a.reload.customers.first
      pp customer.vendor # => nil **(expecting `a`)**
      pp customer.company # => `a` **(expecting `b`)**
      

            Assignee:
            Unassigned Unassigned
            Reporter:
            gerad Gerad Suyderhoud
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: