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

Document primary_key/foreign_key

    • Type: Icon: Improvement Improvement
    • Resolution: Fixed
    • Priority: Icon: Major - P3 Major - P3
    • 7.1.0.rc0
    • Affects Version/s: None
    • Component/s: Docs

      https://docs.mongodb.com/mongoid/master/tutorials/mongoid-relations/ has no mention of `primary_key`, and does mention `foreign_key` but does not explain how to use it.

      I expected the following to work but it does not:

      class Company
        include Mongoid::Document
      
        has_many :emails, primary_key: 'e', foreign_key: 'e_id'
      end
      
      class Email
        include Mongoid::Document
      
        belongs_to :company, primary_key: 'e', foreign_key: 'e_id'
      end
      
      irb(main):001:0> c=Company.create!
      => #<Company _id: 5bd2015bce4ef3495e605040, >
      irb(main):002:0> Email.create!(company:c)
      Traceback (most recent call last):
              1: from (irb):2
      NoMethodError (undefined method `e' for #<Company _id: 5bd2015bce4ef3495e605040, >)
      

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: