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

Don't put embedded object into other model as embedded object too

    • Type: Icon: Bug Bug
    • Resolution: Won't Fix
    • Priority: Icon: Major - P3 Major - P3
    • 7.0.0
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None
    • 2

      I've got 3 Models:

      .../models/book/work.rb

      class Book::Work

      include Mongoid::Document
      include Mongoid::Timestamps

      embedded_in :category, class_name: '::Book::Work::Category', inverse_of: :book_works
      embedded_in :package, class_name: '::Book::Package', inverse_of: :works

      field :name_full, type: String

      end


      .../models/book/work/category.rb

      class Book::Work::Category

      include Mongoid::Document
      include Mongoid::Timestamps

      field :name, type: String

      embeds_many :book_works, class_name: '::Book::Work', inverse_of: :categories

      end

      ____
      ..../models/book/package.rb

      class Book::Package
      include Mongoid::Document
      include Mongoid::Timestamps

      field :name_short, type: String
      field :name_full, type: String
      field :name_abbr, type: String
      field :name_market, type: String
      field :instruction, type: String
      field :cost, type: Float

      embeds_many :works, class_name: '::Book::Work', inverse_of: :package

      end


      What I do: rails console:

      Book::Category.create(name:'Category')
      Book::Category.last.works.create(name_full:'Work')
      work = Book::Category.last.works.last
      Book::Package.create(name_full:'Package')
      package = Book::Package.last
      package.works << work
      =

      {The work I've put}

      package.save
      Book::Package.last.works
      = []
      But if I do this twice:
      " package.works << work "
      it will add this work once to my Book::Package.last.works. Also when I do like this:
      " package.works << Book::Work.new(name_full: 'Work') "

      then it works.

      I use Rails 4.1.8, 4.2.0; Mongoid 4.0.0, 4.0.2; Ruby 2.15, 2.2.0; Mongod 2.6.6, 2.6.7, 2.4.13, 3.0 - all the same..

            Assignee:
            emily.stolfo Emily Stolfo
            Reporter:
            Fusion23 Fusion23 [X]
            Votes:
            0 Vote for this issue
            Watchers:
            1 Start watching this issue

              Created:
              Updated:
              Resolved: