ruby-on-railsrubyrecursionruby-on-rails-6accepts-nested-attributes

Recursive accepts_nested_attributes_for in Rails, is it possible?


I want to create a tree structure between two models, Bar and Foo.

Bar has many Foos

    Bar
  /  |  \
Foo Foo Foo
         ¦
        Bar
      /  |  \
    Foo Foo Foo

Bar can optionally belong to Foo.

Bar has many Foos, to infinity and beyond...

I configured things like this, but it doesn't seem to seed properly.

Even if I comment out any validations I have, I get the following error:

ActiveRecord::RecordInvalid: Validation failed: Foos bar must exist

I can't understand why.

class Bar < ApplicationRecord
  has_many :foos, inverse_of: :bar

  accepts_nested_attributes_for :foos
end


class Foo < ApplicationRecord
  belongs_to :bar, inverse_of: :foos

  accepts_nested_attributes_for :bar
end


class CreateFoos < ActiveRecord::Migration[6.1]
  def change
    create_table :foos do |t|
      t.text :description, null: false

      t.timestamps
    end
  end
end

class CreateBars < ActiveRecord::Migration[6.1]
  def change
    create_table :bars do |t|
      t.text :description

      t.references :foo,
        foreign_key: true,
        null: true,
        on_delete: :cascade,
        on_update: :cascade

      t.timestamps
    end
  end
end

class AddBarIdToFoosTable < ActiveRecord::Migration[6.1]
  def change
    add_reference :foos,
      :bar,
      foreign_key: true,
      null: false,
      on_delete: :cascade,
      on_update: :cascade
  end
end


Bar.create!([
  {
    description: 'Lorem ipsum...',
    foos_attributes: [
      {
        description: 'Lorem ipsum...',
        bar_attributes: {
          description: 'Lorem ipsum...',
          foos_attributes: [
            {
              description: 'Lorem ipsum...',
              bar_attributes: {
                description: 'Lorem ipsum...',
                foos_attributes: [
                  {
                    description: 'Lorem ipsum...'
                  },
                  {
                    description: 'Lorem ipsum...'
                  },
                  {
                    description: 'Lorem ipsum...'
                  },
                  {
                    description: 'Lorem ipsum...'
                  }
                ]
              }
            }
          ]
        }
      }
    ]
  }
])

Solution

  • ActiveRecord::RecordInvalid: Validation failed: Foos bar must exist

    Ref: https://github.com/rails/rails/pull/18937/files