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

Mongoid::Errors::UnknownAttribute on rails development mode

    • Type: Icon: Task Task
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • 12_01_17
    • Affects Version/s: None
    • Component/s: None
    • Labels:
      None

      It happens on Mongoid 3.1.6 with Rails 3.2.16. Here are the steps to reproduce:

      1. we have some inheritance models with accepts_nested_attributes_for option (see the full example: https://github.com/zuzara/test-rails-mongoid-inheritance/tree/master/app/models)
      2. run rails server (development mode)
      3. create Product through your browser (access /products)
      4. there should be no error
      5. edit any model file
      6. create Product again
      7. get Mongoid::Errors::UnknownAttribute

      It happens on Product.new({detail_attributes: {_type: "Laptop", price: 10)}}
      Laptop.superclass == Detail will be false when it gets reloaded. I see superclass mismatch for class Laptop error, then Mongoid::Errors::UnknownAttribute is threw.

      Mongoid seems modifying superclass in lib/mongoid/hierarchy.rb, maybe that's why the model can't be overwritten but I couldn't figure out the exact reason. The problem will be gone if I add require_dependency for subclasses in the parent class. (in detail.rb in the example)

      Am I missing something or is the behavior just as designed? I've found this post on Rails side, so wasn't sure if it's a bug - https://github.com/rails/rails/issues/8699

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

              Created:
              Updated:
              Resolved: