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

has_and_belongs_to_many with inverse_of and validate seems to be broken

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

      This only happens when loading existing Object from database.

      class Site
        include Mongoid::Document
        field :name, :type => String
      
        has_and_belongs_to_many :emails, inverse_of: :sites, class_name: 'Email'
        belongs_to :owner, inverse_of: :owner_of, class_name: 'Email'
      
        validates :emails, :presence => { :message => "error" }
      
      end
      
      class Email
        include Mongoid::Document
        field :address, :type => String
      
        has_and_belongs_to_many :sites, inverse_of: :emails, class_name: 'Site'
        has_many :owner_of, inverse_of: :owner, class_name: 'Site'
      
      end
      

      Rails console:
      1. > Site.create(name: "ups", emails: [Email.create(address:"test@test.com")])
      2. > site = Site.all.last
      3. > site.save!
      Result: I get a validation error telling me emails is not set

      Rails console:
      1. > Site.create(name: "ups", emails: [Email.create(address:"test@test.com")])
      2. > site = Site.all.last
      3. > site.emails
      4. > site.save!
      Result: true

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

              Created:
              Updated:
              Resolved: