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

hab2m relations in 2.3.3 bug

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

      Hey folks,

      It seems there is a bug with hab2m relations appeared in 2.3.3. Say I've got models like that

      class Model
         has_and_belongs_to_many :stuff
      
        field :text, type: String
      
        def text=(text)
           self.stuff = parse_stuff_from(text)
           super(text)
        end
      end
      

      Meaning I'm assigning the stuff hab2m association by hijacking one of the attributes writer.

      Then when i do the usual stuff in the controller

      class ModelController
        def create
          @model= Model.new(params[:model])
        end
      end
      

      It crashes like that

      Unable to find source-code formatter for language: text. Available languages are: actionscript, ada, applescript, bash, c, c#, c++, cpp, css, erlang, go, groovy, haskell, html, java, javascript, js, json, lua, none, nyan, objc, perl, php, python, r, rainbow, ruby, scala, sh, sql, swift, visualbasic, xml, yaml
      mongoid (2.3.3) lib/mongoid/relations/referenced/many_to_many.rb:37:in `block (3 levels) in <<'
      mongoid (2.3.3) lib/mongoid/relations/referenced/many_to_many.rb:30:in `each'
      mongoid (2.3.3) lib/mongoid/relations/referenced/many_to_many.rb:30:in `block (2 levels) in <<'
      mongoid (2.3.3) lib/mongoid/relations/referenced/many_to_many.rb:29:in `tap'
      mongoid (2.3.3) lib/mongoid/relations/referenced/many_to_many.rb:29:in `block in <<'
      mongoid (2.3.3) lib/mongoid/relations/referenced/batch.rb:65:in `count_executions'
      mongoid (2.3.3) lib/mongoid/relations/referenced/batch.rb:42:in `batched'
      mongoid (2.3.3) lib/mongoid/relations/referenced/many_to_many.rb:28:in `<<'
      mongoid (2.3.3) lib/mongoid/relations/referenced/many.rb:298:in `block in substitute'
      mongoid (2.3.3) lib/mongoid/relations/referenced/many.rb:295:in `tap'
      mongoid (2.3.3) lib/mongoid/relations/referenced/many.rb:295:in `substitute'
      mongoid (2.3.3) lib/mongoid/relations/accessors.rb:128:in `block (2 levels) in setter'
      app/models/opinion.rb:81:in `text='
      

      It woke just fine before 2.3.3 and works fine if I do it like that in the controller

      def create
        @model= Model.new
        @model.attributes = params[:model]
         ....
      end
      

      After digging a bit into your code, it seems that the association accessor method returns nil instead of an empty array at the moment when the text attribute is getting assigned in the constructor.

      Could you fix that please?
      Thanks

            Assignee:
            Unassigned Unassigned
            Reporter:
            MadRabbit MadRabbit [X]
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved: