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

Excessive query when saving with has_one relation.

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

      Le't s have a has_one relation with autosave: true and validate: false.

      class Foo
      include Mongoid::Document
      has_one :bar, autosave: true, validate: false
      end

      class Bar
      include Mongoid::Document
      belongs_to :foo
      end

      >> foo = Foo.first
      foo = Foo.first
      MOPED: 127.0.0.1:27017 QUERY database=gocheap_development collection=foos selector={"$query"=>{}, "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.4294ms)
      #<Foo _id: 512c4ba25b9e0528b5000002, >

      >> foo.save
      MOPED: 127.0.0.1:27017 QUERY database=gocheap_development collection=bars selector={"$query"=>

      {"foo_id"=>"512c4ba25b9e0528b5000002"}

      , "$orderby"=>{:_id=>1}} flags=[:slave_ok] limit=-1 skip=0 batch_size=nil fields=nil (0.8771ms)
      true

      So for some reason it makes a query for bar. But we have validate: false, and as the relation hasn't been loaded, there's obviously nothing to save in the bar object.

            Assignee:
            arthurnn Arthur Nogueira Neves
            Reporter:
            exoth Yuriy Trofimenko
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: