Perry Smith


50 Experience
0 Lessons Completed
0 Questions Solved


Files is an Active Record model that has a reference to its parent of the same type:

class Files < ActiveRecord::Base
  belongs_to :parent, class_name: "Files"

I want to create a root entry whose parent is itself (which mimics the root directory which has .. that points to itself).

I have tried several various things most of which end up trying to save the record to the database with the parent still set to null. The db is set up to throw an exception if that field is null. I've tried: { |f|
  f.parent   = f


Files.create { |f|
  f.parent   = f

I've also tried { |f|
  f.parent_id = = Files.next_sequence_value

And I have looked at build_parent

logo Created with Sketch.

Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more. Icons by Icons8

© 2022 GoRails, LLC. All rights reserved.