Error when running `rails g scaffold Photo`: `undefined method `all' for module ActiveModel (NoMethodError)`

Versions:

  • ruby: 3.3.5
  • rails: 7.2.1

I’m receiving the following error when trying to run rails g scaffold Photo

gems/jbuilder-2.13.0/lib/generators/rails/templates/controller.rb:11:in block in template': undefined method all' for module ActiveModel (NoMethodError)

Full Stack Trace
      invoke  active_record
      create    db/migrate/20240927035144_create_photos.rb
      create    app/models/photo.rb
      invoke    test_unit
      create      test/models/photo_test.rb
      create      test/fixtures/photos.yml
      invoke  resource_route
       route    resources :photos
      invoke  scaffold_controller
Users/prometheus/.gem/ruby/3.3.5/gems/jbuilder-2.13.0/lib/generators/rails/templates/controller.rb:11:in `block in template': undefined method `all' for module ActiveModel (NoMethodError)
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/actions/file_manipulation.rb:347:in `block in capture'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/actions/file_manipulation.rb:354:in `with_output_buffer'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/actions/file_manipulation.rb:347:in `capture'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/railties-7.2.1/lib/rails/generators/base.rb:297:in `module_namespacing'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/jbuilder-2.13.0/lib/generators/rails/templates/controller.rb:5:in `template'
        from /Users/prometheus/.rubies/ruby-3.3.5/lib/ruby/3.3.0/erb.rb:429:in `eval'
        from /Users/prometheus/.rubies/ruby-3.3.5/lib/ruby/3.3.0/erb.rb:429:in `result'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/actions/file_manipulation.rb:128:in `block in template'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/actions/create_file.rb:54:in `render'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/actions/create_file.rb:47:in `identical?'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/actions/create_file.rb:74:in `on_conflict_behavior'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/actions/empty_directory.rb:115:in `invoke_with_conflict_check'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/actions/create_file.rb:61:in `invoke!'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/actions.rb:93:in `action'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/actions/create_file.rb:25:in `create_file'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/actions/file_manipulation.rb:124:in `template'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/railties-7.2.1/lib/rails/generators/named_base.rb:25:in `block in template'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/railties-7.2.1/lib/rails/generators/named_base.rb:45:in `inside_template'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/railties-7.2.1/lib/rails/generators/named_base.rb:24:in `template'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/railties-7.2.1/lib/rails/generators/rails/scaffold_controller/scaffold_controller_generator.rb:24:in `create_controller_files'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/command.rb:28:in `run'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/invocation.rb:127:in `invoke_command'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/invocation.rb:134:in `block in invoke_all'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/invocation.rb:134:in `each'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/invocation.rb:134:in `map'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/invocation.rb:134:in `invoke_all'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/group.rb:243:in `dispatch'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/invocation.rb:116:in `invoke'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/group.rb:288:in `block in _invoke_for_class_method'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/shell.rb:68:in `with_padding'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/group.rb:277:in `_invoke_for_class_method'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/group.rb:134:in `_invoke_from_option_scaffold_controller'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/command.rb:28:in `run'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/invocation.rb:127:in `invoke_command'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/invocation.rb:134:in `block in invoke_all'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/invocation.rb:134:in `each'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/invocation.rb:134:in `map'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/invocation.rb:134:in `invoke_all'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/group.rb:243:in `dispatch'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/base.rb:584:in `start'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/railties-7.2.1/lib/rails/generators.rb:261:in `invoke'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/railties-7.2.1/lib/rails/commands/generate/generate_command.rb:26:in `perform'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/command.rb:28:in `run'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor/invocation.rb:127:in `invoke_command'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/railties-7.2.1/lib/rails/command/base.rb:178:in `invoke_command'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/thor-1.3.2/lib/thor.rb:538:in `dispatch'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/railties-7.2.1/lib/rails/command/base.rb:73:in `perform'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/railties-7.2.1/lib/rails/command.rb:71:in `block in invoke'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/railties-7.2.1/lib/rails/command.rb:149:in `with_argv'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/railties-7.2.1/lib/rails/command.rb:69:in `invoke'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/railties-7.2.1/lib/rails/commands.rb:18:in `<main>'
        from /Users/prometheus/.rubies/ruby-3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `require'
        from /Users/prometheus/.rubies/ruby-3.3.5/lib/ruby/3.3.0/bundled_gems.rb:75:in `block (2 levels) in replace_require'
        from /Users/prometheus/.gem/ruby/3.3.5/gems/bootsnap-1.18.4/lib/bootsnap/load_path_cache/core_ext/kernel_require.rb:30:in `require'
        from bin/rails:4:in `<main>'

It appears to happen when the generator gets to the scaffold_controller command.

What’s also strange is that running bin/rails g scaffold_controller Photo by itself works fine.

I’m a realtively new to rails (but have 10 years experience in other stacks) so I’m confused what’s going on. The most elaborate thing I’ve done is use the brick gem to scaffold some controllers and models from an existing db. Other than that it’s a pretty fresh rails app.

This does not happen on a totally new rails app, so there is something funky with my setup.

Let me know if there is any other details to providde, thanks!!

Please share your Gemfile and config/application.rb

application.rb
require_relative "boot"

require "rails/all"

# Require the gems listed in Gemfile, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(*Rails.groups)

module SvsRails
  class Application < Rails::Application
    # Initialize configuration defaults for originally generated Rails version.
    config.load_defaults 7.2

    # Please, add to the `ignore` list any other `lib` subdirectories that do
    # not contain `.rb` files, or that should not be reloaded or eager loaded.
    # Common ones are `templates`, `generators`, or `middleware`, for example.
    config.autoload_lib(ignore: %w[assets tasks])

    # Configuration for the application, engines, and railties goes here.
    #
    # These settings can be overridden in specific environments using the files
    # in config/environments, which are processed later.
    #
    # config.time_zone = "Central Time (US & Canada)"
    # config.eager_load_paths << Rails.root.join("extras")
  end
end
Gemfile
source "https://rubygems.org"

# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.2.1"
# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails"
# Use sqlite3 as the database for Active Record
gem "sqlite3", ">= 1.4"
# Use the Puma web server [https://github.com/puma/puma]
gem "puma", ">= 5.0"
# Bundle and transpile JavaScript [https://github.com/rails/jsbundling-rails]
gem "jsbundling-rails"
# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails"
# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"
# Bundle and process CSS [https://github.com/rails/cssbundling-rails]
gem "cssbundling-rails"
# Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem "jbuilder"
# Use Redis adapter to run Action Cable in production
gem "redis", ">= 4.0.1"

# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]
# gem "kredis"

# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"

# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ windows jruby ]

# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false

# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"

group :development, :test do
  # See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
  gem "debug", platforms: %i[ mri windows ], require: "debug/prelude"

  # Static analysis for security vulnerabilities [https://brakemanscanner.org/]
  gem "brakeman", require: false

  # Omakase Ruby styling [https://github.com/rails/rubocop-rails-omakase/]
  gem "rubocop-rails-omakase", require: false
end

group :development do
  # Use console on exceptions pages [https://github.com/rails/web-console]
  gem "web-console"
end

group :test do
  # Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
  gem "capybara"
  gem "selenium-webdriver"
end

gem "dockerfile-rails", ">= 1.6", group: :development

gem "aws-sdk-s3", "~> 1.163", require: false

gem "hotwire-livereload", "~> 1.4", group: :development

gem "dotenv", groups: [:development, :test]

gem "meta-tags"

gem "rails_heroicon"

gem "brick"

gem "pg"

gem "shrine", "~> 3.0"

mmm, I just created a new app with the same gems and ran the command. I could not reproduce the error. my guess is that there is something fishy in the ApplicationRecord or the config/initializers, but it is really hard to say. Is the repository openly accessible?

The repo is currently private, but if you’re willing I wouldn’t mind adding you as a collaborator.

If you’re up for that would you mind sharing your github profile with me?

Otherwise are there any other files I could post which would help debug?

Also since you mentioned them, here is the entirety of my application_record.rb

class ApplicationRecord < ActiveRecord::Base
  primary_abstract_class
end

And here are the initializers that have content (a few are completely commented out)

shrine.rb
require "shrine"
require "shrine/storage/memory"
require "shrine/storage/s3"

s3_options = {
  bucket: ENV["BUCKET_NAME"] || "fake", # required
  region: "auto", # required
  access_key_id: ENV["AWS_ACCESS_KEY_ID"] || "fake", # required
  secret_access_key: ENV["AWS_SECRET_ACCESS_KEY"], # required
  endpoint: ENV["AWS_ENDPOINT_URL_S3"] || "https://fly.storage.tigris.dev",
}

if Rails.env.test?
  Shrine.storages = {
    cache: Shrine::Storage::Memory.new,
    store: Shrine::Storage::Memory.new,
  }
else
  Shrine.storages = {
    cache: Shrine::Storage::S3.new(prefix: "cache", **s3_options),
    store: Shrine::Storage::S3.new(prefix: "store", **s3_options),
  }
end

Shrine.plugin :activerecord
Shrine.plugin :cached_attachment_data # for retaining the cached file across form redisplays
Shrine.plugin :restore_cached_data # re-extract metadata when attaching a cached file
Shrine.plugin :validation
Shrine.plugin :validation_helpers
filter_parameter_logging.rb
Rails.application.config.filter_parameters += [
  :passw, :email, :secret, :token, :_key, :crypt, :salt, :certificate, :otp, :ssn
]
assets.rb
Rails.application.config.assets.version = "1.0"

I have also tried totaly disabling brick and that didn’t have any effect

brick.rb
# frozen_string_literal: true

# Settings for the Brick gem
# (By default this auto-creates models, controllers, views, and routes on-the-fly.)

if ActiveRecord::Base.respond_to?(:brick_select) && !::Brick.initializer_loaded

  Brick.mode = :development

  # Brick.mode = :on # (or :off to entirely disable)

  # # Custom path prefix to apply to all auto-generated Brick routes.  Also causes auto-generated controllers
  # # to be created inside a module with the same name.
  ::Brick.path_prefix = 'brick'

end

Thanks for sharing. I can not see any red flags from these snippets. Maybe it would be best if I checked out the code and take a look. Here my GitHub handle: motine

Awesome, just added you, really appreciate you taking the time to look at it.

There is now a Photo model because I ran the generators individually, but you should be able to reproduce it by trying with a different model.

alrighty, I checked out the code and found something. Turns out your initial instinct was correct and brick is actually the culprit. I am not sure how much you need brick. If you need it, maybe investigate if the gem is setup correctly in your repo or dig deeper into their code or open an issue with them. There are also alternatives such as ActiveAdmin with similar functionality. Here the video version of this message.

1 Like

Holy moly you went above and beyond thank you!!

Your find led me to discovering the exact culprit in the brick initializer:

  ::Brick.path_prefix = 'brick'

this prefixes the brick generated routes with /brick. When I comment it out the generators work again.

For context I’m using brick as a way to adapt rails to an existing database more so than an admin panel, so it’s crucial in the near term but I can probably remove it later.

Either way I will definitely file a ticket with the brick repo, thanks again for your assistance!

Cheers!

(I’m also seriously considering purchasing cleanshot, looks like a slick app! :smile: )

2 Likes

Heya Tom and Sean – thanks for finding this issue! It has now been fixed in release v1.0.218 of Brick with this commit.

Thanks so much!

-Lorin

2 Likes