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

has_and_belongs_to_many with inverse_of: nil seems broken

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

      Hi,

      See https://github.com/mongoid/mongoid/issues/1838 for a description of my setup. But using Mongoid 2.4.6 I get errors with the following:

      class Game
        include Mongoid::Document
        has_and_belongs_to_many :users, inverse_of: nil
      end
      
      class User
        include Mongoid::Document
      end
      
      g = Game.first
      u = User.first
      
      g.users << u
      g.save!
      

      Mongoid::Errors::Validations: Validation failed - Users is invalid.
      from /Users/joel/.rvm/gems/ruby-1.9.2-p180@joel/bundler/gems/mongoid-08170f553f1c/lib/mongoid/persistence.rb:254:in `fail_validate!'
      from /Users/joel/.rvm/gems/ruby-1.9.2-p180@joel/bundler/gems/mongoid-08170f553f1c/lib/mongoid/persistence.rb:76:in `save!'
      from (irb):12
      from /Users/joel/.rvm/gems/ruby-1.9.2-p180@joel/gems/railties-3.2.2/lib/rails/commands/console.rb:47:in `start'
      from /Users/joel/.rvm/gems/ruby-1.9.2-p180@joel/gems/railties-3.2.2/lib/rails/commands/console.rb:8:in `start'
      from /Users/joel/.rvm/gems/ruby-1.9.2-p180@joel/gems/railties-3.2.2/lib/rails/commands.rb:41:in `<top (required)>'
      from script/rails:6:in `require'
      from script/rails:6:in `<main>'

      This also happens if I use g.users.push etc.

      The only way I can get this to work is

      g.user_ids << u.id

      but that seems to break the abstraction of relations ...

      Any ideas?

      Thanks!

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

              Created:
              Updated:
              Resolved: