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

Nested attributes, destroy and 3.0.10

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

      Hi,

      I have an issue for mongoid 3.0.10 that was not present in 3.0.9. I think it's related to MONGOID-2367 but I don't know how to update my models to reflect new behaviour changes

      So we have

      Unable to find source-code formatter for language: rb. 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
      class User
        include Mongoid::Document
        include Mongoid::Timestamps
        include Mongoid::MultiParameterAttributes
        include Mongoid::Paranoia
      
        embeds_many :albums, cascade_callbacks: true
        accepts_nested_attributes_for :albums, allow_destroy: true
      
        field :name
      end
      
      class Album
        include Mongoid::Document
        include Mongoid::Timestamps
        include Mongoid::Paranoia
      
        attr_accessible :photos_attributes, :name
      
        embedded_in :user
        embeds_many :photos, cascade_callbacks: true
        accepts_nested_attributes_for :photos, allow_destroy: true
      
        field :name
      
        validates :name, uniqueness: true
      end
      
      class Photo
        include Mongoid::Document
      
        embedded_in :album
      
        attr_accessible :caption
      
        field :caption
      end
      

      we build objects this way

      Unable to find source-code formatter for language: rb. 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
        user = User.new
        user.name = "Bob"
        user.save
        album = user.albums.create(name: "My first album")
        album.photos.create(caption: "First photo")
        album.photos.create(caption: "Second photo")
      

      this is our view

      Unable to find source-code formatter for language: haml. 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
      = form_for @user do |f|
        = f.fields_for :albums, @user.albums.first do |album_form|
          = album_form.fields_for :photos, @user.albums.first.photos do |photos_form|
            = photos_form.text_field :caption
            %br
            = photos_form.label :_destroy do
              = photos_form.check_box :_destroy
              Destroy
            %hr
        = f.submit
      

      and our controller

      Unable to find source-code formatter for language: rb. 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
        def update
          puts params[:user]
          @user = User.first
          if @user.update_attributes(params[:user])
            render text: "OK"
          else
            render text: "ERROR"
          end
        end
      

      I got an "OK" with both 3.0.10 and 3.0.9 but 3.0.10 doesn't destroy photos.

      This is caused by the double nesting? I'm missing something?

      As usual, TIA

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

              Created:
              Updated:
              Resolved: