Business
Jobs
  • About Us
  • Solutions
    • Job Postings
      Post your job and receive qualified candidates in 48h.
    • Candidate Assessments
      500+ technical and psychological tests, plus anti-fraud.
    • Headhunting
      Tailor-made executive search from start to finish.
    • Payroll + EOR
      Payroll dispersal and EOR across 15+ LATAM countries.
  • Pricing
  • Jobs

0

299
Views
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...'
                  }
                ]
              }
            }
          ]
        }
      }
    ]
  }
])
over 4 years ago · Santiago Trujillo
1 answers
Answer question

0

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

over 4 years ago · Santiago Trujillo Report
Answer question
Find remote jobs

Discover the new way to find a job!

Top jobs
Top job categories
Business
Post vacancy Pricing Sales
Legal
Terms and conditions Privacy policy
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Show me some job opportunities
There's an error!