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

belongs_to association not saving object ID on `create_#{association}` method call

    • Type: Icon: Task Task
    • Resolution: Gone away
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 7.0.4
    • Component/s: Associations
    • Labels:
      None

      I'm experiencing some weirdness that didn't seem to be the case in ActiveRecord. Note that I'm working with a legacy database so I need to assign the foreign key on the `users` collection.

       

      ```
      class User
      include Mongoid::Document
      include Mongoid::Timestamps

      belongs_to :company, foreign_key: "companyId"
      end

      class Company
      include Mongoid::Document
      include Mongoid::Timestamps

      has_many :users
      end
      ```
      Ok that all looks good to me. But when I do the following on the console, a `Company` is created, but the `User` isn't saved with the `companyId` set on it.

      ```
      user.create_company(name: "My cool company")
      ```

      Instead I have to add a `#save` call on `user` like so...

      ```
      user.create_company(name: "My cool company")
      user.save
      ```

      Shouldn't `create_company` save the `User` record?

            Assignee:
            oleg.pudeyev@mongodb.com Oleg Pudeyev (Inactive)
            Reporter:
            mail@joncrawford.com Jonathan Crawford
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: