Search code examples
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

    • This is telling you that one of your Foo declarations requires the presence of bar
    • The reference to bar in your model declaration for Foo is in the belongs_to association
    • belongs_to is presence-validated by default in certain versions of rails; changing belongs_to :bar to belongs_to :bar, optional: true will likely resolve your issue

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