bullet_train-core icon indicating copy to clipboard operation
bullet_train-core copied to clipboard

Show a reasonable error message for self-referential models in super scaffolding

Open jagthedrummer opened this issue 9 months ago • 0 comments

Fixes: https://github.com/bullet-train-co/bullet_train-core/issues/821

Previously if you tried to super scaffold a model that references itself like this:

rails generate super_scaffold Scrape Team scrape_id:super_select

You'd get an error like this:

active_support/inflector/methods.rb:290:in `constantize': uninitialized constant Scrape (NameError)

      Object.const_get(camel_cased_word)
            ^^^^^^^^^^
	from .../activesupport-7.1.3.2/lib/active_support/core_ext/string/inflections.rb:74:in `constantize'
	from .../bullet_train-super_scaffolding-1.6.38/lib/scaffolding/script.rb:146:in `block in check_required_options_for_attributes'

This PR makes it so that we alert the user to the fact that they're trying to generate a self-referential model and we suggest using a two-step process to do what they want to do.

You appear to be trying to scaffold a model that references itself. Unfotunately this needs to be a two-step process.
First you should generate the model without the reference, and then add the reference as a new field. For instance:

  rails generate super_scaffold Scrape Team
  rails generate super_scaffold:field Scrape scrape_id:super_select

If `scrape_id` is just a regular field and isn't backed by an ActiveRecord association, you can skip all this with the `{vanilla}` option, e.g.:

  rails generate super_scaffold Scrape Team scrape_id:super_select{vanilla}

jagthedrummer avatar May 23 '24 20:05 jagthedrummer