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

Object automatically added to containing object, even if invalid

    XMLWordPrintable

    Details

    • Type: Task
    • Status: Closed
    • Resolution: Done
    • Affects Version/s: None
    • Fix Version/s: 3.1.0
    • Component/s: None
    • Labels:
      None

      Description

      Consider the following:

      class User
        include Mongoid::Document
       
        embeds_many :accounts
       
      end
       
      class Account
        include Mongoid::Document
       
        embedded_in :user
       
        field :name, :type String
        validates_presence_of :name
       
      end
       
      u = User.find('xxx')
      u.accounts.to_a.count # 0
      a = Account.new(:user => u)
      a.valid? # false
      u.accounts.to_a.count # 1
      
      

      This creates all sorts of headaches when rendering a view which includes the Account instance being altered AND the collection of Account instances (as the new Account object appears in u.accounts).

      I'm pretty sure this doesn't happen with AR as u.accounts would execute a fetch from the DB.

      My only workaround is to run u.reload before retrieving the collection of items.

        Attachments

          Activity

            People

            Assignee:
            Unassigned
            Reporter:
            jyn jyn
            Participants:
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Dates

              Created:
              Updated:
              Resolved: