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

Two belongs_to, one with has_one, one with has_many gives nil error

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

      With the following models defined:

      class Account
        include Mongoid::Document
        has_many :editorials
      end
      
      class FeedItem
        include Mongoid::Document
        has_one :editorial
      end
      
      class Editorial
        include Mongoid::Document
        belongs_to :feed_item
        belongs_to :account
      end
      

      and e, a and {{fi}}saved instances of these models, when I run the following code:

      e.account = a
      e.feed_item = fi
      

      I get the following error:

      /Users/spatrik/.rvm/gems/ruby-1.8.7-p174@mp3rails3/bundler/gems/mongoid-bf30e802fdb1/lib/mongoid/relations/bindings/referenced/in.rb:32:in `bind_one': undefined method `push' for nil:NilClass (NoMethodError)
      	from /Users/spatrik/.rvm/gems/ruby-1.8.7-p174@mp3rails3/bundler/gems/mongoid-bf30e802fdb1/lib/mongoid/relations/binding.rb:21:in `binding'
      	from /Users/spatrik/.rvm/gems/ruby-1.8.7-p174@mp3rails3/bundler/gems/mongoid-bf30e802fdb1/lib/mongoid/relations/bindings/referenced/in.rb:23:in `bind_one'
      	from /Users/spatrik/.rvm/gems/ruby-1.8.7-p174@mp3rails3/bundler/gems/mongoid-bf30e802fdb1/lib/mongoid/relations/proxy.rb:20:in `__send__'
      	from /Users/spatrik/.rvm/gems/ruby-1.8.7-p174@mp3rails3/bundler/gems/mongoid-bf30e802fdb1/lib/mongoid/relations/proxy.rb:20:in `bind_one'
      	from /Users/spatrik/.rvm/gems/ruby-1.8.7-p174@mp3rails3/bundler/gems/mongoid-bf30e802fdb1/lib/mongoid/relations/referenced/in.rb:24:in `initialize'
      	from /Users/spatrik/.rvm/gems/ruby-1.8.7-p174@mp3rails3/bundler/gems/mongoid-bf30e802fdb1/lib/mongoid/relations/proxy.rb:35:in `init'
      	from /Users/spatrik/.rvm/gems/ruby-1.8.7-p174@mp3rails3/bundler/gems/mongoid-bf30e802fdb1/lib/mongoid/relations/referenced/in.rb:22:in `initialize'
      	from /Users/spatrik/.rvm/gems/ruby-1.8.7-p174@mp3rails3/bundler/gems/mongoid-bf30e802fdb1/lib/mongoid/relations/accessors.rb:44:in `new'
      	from /Users/spatrik/.rvm/gems/ruby-1.8.7-p174@mp3rails3/bundler/gems/mongoid-bf30e802fdb1/lib/mongoid/relations/accessors.rb:44:in `create_relation'
      	from /Users/spatrik/.rvm/gems/ruby-1.8.7-p174@mp3rails3/bundler/gems/mongoid-bf30e802fdb1/lib/mongoid/relations/accessors.rb:26:in `build'
      	from /Users/spatrik/.rvm/gems/ruby-1.8.7-p174@mp3rails3/bundler/gems/mongoid-bf30e802fdb1/lib/mongoid/relations/accessors.rb:145:in `feed_item='
      	from mongoid-bug3.rb:38
      

      However, when I switch the order of the assignments everything works as it's supposed:

      e.feed_item = fi
      e.account = a
      

      It seems to be that the referenced_many? here returns true, even though it's a has_one relation. Is this something I'm missing or is this a bug?

      The problem occur in both master and 2.1.2

            Assignee:
            Unassigned Unassigned
            Reporter:
            spatrik Patrik Stenmark
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: