padrino/padrino-framework · error · Padrino::Admin::Generators::OrmError

Model '#{klass_name}' could not be found! Perhaps you would

Error message

Model '#{klass_name}' could not be found!
Perhaps you would like to run 'bundle exec padrino g model #{klass_name}' to create it first?

What it means

Padrino::Admin::Generators::Orm wraps a model for the admin/admin_page generators. At initialize it constantizes the underscored/camelized name (rescuing to nil) and raises OrmError when the class cannot be resolved and no explicit columns were passed — the generator cannot introspect a model that does not exist. The message names the exact model generator command to run first.

Source

Thrown at padrino-admin/lib/padrino-admin/generators/orm.rb:25

      # Defines a generic exception for the admin ORM handler.
      class OrmError < StandardError; end

      ##
      # Defines the generic ORM management functions used to manipulate data for admin.
      class Orm
        attr_reader :klass_name, :klass, :name_plural, :name_singular, :orm, :name_param

        def initialize(name, orm, columns = nil, column_fields = nil)
          name            = name.to_s
          @klass_name     = name.underscore.camelize
          @klass          = @klass_name.constantize rescue nil
          @name_param     = name.underscore.gsub('/', '_')
          @name_singular  = name.underscore.gsub(%r{^.*/}, '') # convert submodules i.e. FooBar::Jank.all # => jank
          @name_plural    = @name_singular.pluralize
          @orm            = orm.to_sym
          @columns        = columns
          @column_fields  = column_fields
          raise OrmError, "Model '#{klass_name}' could not be found!\nPerhaps you would like to run 'bundle exec padrino g model #{klass_name}' to create it first?" if @columns.nil? && @klass.nil?
        end

        def activerecord?
          case orm
          when :activerecord, :minirecord then true
          else false
          end
        end

        def field_type(type)
          type = :string if type.nil?
          type = type.to_s.demodulize.downcase.to_sym unless type.is_a?(Symbol)

          case type
          when :integer, :float, :decimal   then :text_field
          when :string                      then :text_field
          when :text                        then :text_area
          when :boolean                     then :check_box

View on GitHub (pinned to 167044f3d5)

Solutions

  1. Create the model first: `bundle exec padrino g model Foo`, run migrations, then re-run `padrino g admin_page Foo`
  2. Check the spelling and casing of the name passed to admin_page against the actual class name
  3. For namespaced models, pass a name whose underscore.camelize resolves to the real constant and make sure the file is loaded

Example fix

# before
bundle exec padrino g admin_page Post   # models/post.rb absent

# after
bundle exec padrino g model Post title:string body:text
bundle exec padrino rake ar:migrate
bundle exec padrino g admin_page Post
Defensive patterns

Strategy: validation

Validate before calling

name = 'Post'
if (name.to_s.underscore.camelize.constantize rescue nil).nil?
  abort "run `bundle exec padrino g model #{name}` before generating its admin page"
end

Type guard

def admin_model_loadable?(name)
  klass = (name.to_s.underscore.camelize.constantize rescue nil)
  !klass.nil? && klass.respond_to?(:columns)
end

Try / catch

begin
  Padrino::Admin::Generators::Orm.new('Post', orm)
rescue Padrino::Admin::Generators::OrmError => e
  puts e.message # suggests the exact `padrino g model` command
end

Prevention

When it happens

Trigger: `padrino g admin_page Foo` when models/foo.rb does not exist or does not define class Foo; a typo'd model name; a namespaced model whose constant cannot be resolved from the given name; running the generator outside the app root so model files are not found.

Common situations: Running admin_page before creating or migrating the model; renaming a generated model class without renaming the file; namespaced models (Orm builds the constant via name.underscore.camelize); model file present but empty or class commented out.

Related errors


AI-assisted analysis of padrino/padrino-framework@167044f3d5 (2026-08-23). Data as JSON: /api/errors/ee65966efb5fe534. Report an issue: GitHub.