Search code examples
ruby-on-railsrubyactiveadminrails-activestorage

Rails couldn't find a valid model for ActiveStorage::Blob association, while trying to use activeadmin and active storage


I've:

  1. Set up a rails project

  2. Added Active Admin.

  3. Set up Active Storage, updating the configuration to connect locally and doing:

    bin/rails active_storage:install
    bin/rails db:migrate

Then when trying to set up a very simple model Document with a file attached:

class Document < ApplicationRecord
    has_one_attached :file
end

And the most simple Active Admin page possible for it:

ActiveAdmin.register Document do
  permit_params :name, :type, :file
end

I'm getting the following error when trying to access the page:

Rails couldn't find a valid model for ActiveStorage::Blob association. Please provide the :class_name option on the association declaration. If :class_name is already provided, make sure it's an ActiveRecord::Base subclass.

I'm unsure what is going wrong. It seems from the docs everything should work after the migration and adding that line to the model.

Ruby version: "3.1.1"
Rails version: "7.0.2"


Solution

  • I run into the same issue, and found out that this answer fixed the issue for me: https://stackoverflow.com/a/72005456/583560

    [...]adding an initializer with the following worked (Rails 6.0.4.7):

    Rails.application.reloader.to_prepare do
     ActiveStorage::Blob
    end