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

Mongoid::Errors::ReadonlyAttribute when loading record through relation

    • Type: Icon: Bug Bug
    • Resolution: Duplicate
    • Priority: Icon: Major - P3 Major - P3
    • None
    • Affects Version/s: 6.0.3
    • Component/s: None
    • None

      I have 

       

      class Activity 
        belongs_to :owner, polymorphic: true # e.g. Channel 
        attr_readonly :owner 
      end
      

       

       

      I load a record directly:

       

      2.6.5 :033 > a=Activity.ne(owner_id: nil)[1] => #<Activity _id: 5aeace226665350ec1000004, created_at: 2018-05-03 08:53:54 UTC, updated_at: 2018-05-03 08:53:54 UTC, deleted_at(deleted_at): nil, subject: "Sri Lanka Opportunity Fund - Investment in Kelani Cables up 10.78%", text: "<p>The Sri Lanka Opportunity Fund PTE LTD invested a strategic stake in Kelani Cables PLC. The funds investment is up 10.78% at the end of April 2018. The investment was made in March. </p><p>http://www.ft.lk/front-page/Sri-Lanka-Opportunity-Fund-invests-in-Kelani-Cables/44-653885<br></p>", date: 2018-05-03 08:53:54 UTC, external_id: nil, type_id: BSON::ObjectId('542da77d6265313f38010000'), visible_to_type: nil, visible_to_id: nil, creator_id: BSON::ObjectId('5ae04b3a6265326bdf00235e'), source_type: nil, source_id: nil, owner_type: "ManagerChannel", owner_id: BSON::ObjectId('5ae04caf62653265d7002571')>

       

      but accessing the owner fails:

       

      2.6.5 :034 > a.owner
      Mongoid::Errors::ReadonlyAttribute ()
      message:
        Attempted to set the readonly attribute 'owner_id' with the value: nil.
      summary:
        Attributes flagged as readonly via Model.attr_readonly can only have values set when the document is a new record.
      resolution:
        Don't define 'owner_id' as readonly, or do not attempt to update its value after the document is persisted.
      
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/attributes/readonly.rb:38:in `as_writable_attribute!'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/attributes.rb:166:in `write_attribute'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/fields.rb:462:in `block (2 levels) in create_field_setter'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/extensions/object.rb:104:in `do_or_do_not'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/extensions/object.rb:219:in `you_must'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/relations/binding.rb:102:in `bind_foreign_key'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/relations/bindings/referenced/in.rb:24:in `block in bind_one'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/relations/binding.rb:38:in `block in binding'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/threaded/lifecycle.rb:60:in `_binding'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/relations/binding.rb:37:in `binding'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/relations/bindings/referenced/in.rb:22:in `bind_one'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/relations/proxy.rb:26:in `bind_one'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/relations/referenced/in.rb:43:in `block in initialize'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/relations/proxy.rb:42:in `init'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/relations/referenced/in.rb:41:in `initialize'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/relations/accessors.rb:44:in `new'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/relations/accessors.rb:44:in `create_relation'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/relations/accessors.rb:26:in `__build__'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/relations/accessors.rb:104:in `block (2 levels) in get_relation'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/threaded/lifecycle.rb:130:in `_loading'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/relations/accessors.rb:100:in `block in get_relation'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/threaded/lifecycle.rb:89:in `_building'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/relations/accessors.rb:99:in `get_relation'
      /Users/stanley/.rvm/gems/ruby-2.6.5@fundbase/gems/mongoid-6.0.3/lib/mongoid/relations/accessors.rb:187:in `block in getter'
      

       

      Am I doing something wrong or is this a bug?

            Assignee:
            emily.giurleo@mongodb.com Emily Giurleo (Inactive)
            Reporter:
            stanley90x Marek Stanczyk
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: