evilmartians / evil-seed

A Gem for creating partial anonymized dumps of your database using your app model relations.
MIT License
455 stars 20 forks source link

Model with ActiveStorage attachment can't be anonymized #19

Open billabel opened 1 year ago

billabel commented 1 year ago

When I try to anonymize a model with an active storage attachment. E.g. User with a profile picture

# lib/extensions/evil_seed.rb

require "evil_seed"

EvilSeed.configure do |config|
  config.root("User", id: 13055) do |root|
    root.exclude(/.*/) # exclude all associations
  end

  config.anonymize("User") do
    name { Faker::Name.name }
  end
end

I get a method_missing for has_one_attached on this model.

/Users/billabel/.rbenv/versions/3.2.1/lib/ruby/gems/3.2.0/gems/activerecord-6.1.7.2/lib/active_record/dynamic_matchers.rb:22:in `method_missing': undefined method `has_one_attached' for User:Class (NoMethodError)

Is there a fix or workaround? Is this a ruby 3.2 issue?

Envek commented 1 year ago
undefined method `has_one_attached' for User:Class

That's weird. Probably your application dependencies wasn't required (maybe it is done in one of initializers or whatever)?

Try to initialize your application manually by placing something like this to your lib/extensions/evil_seed.rb:

APP_PATH = File.expand_path('../../config/application', __dir__)
require_relative '../../config/boot'
require_relative '../../config/environment'
billabel commented 1 year ago

Just following back up. I tried the suggestion, but no luck.

For now, I've just moved to pg_dump and seeds, but I'd love to see if I can get this to work. It looks very promising.

Envek commented 1 year ago

Can you please try to replicate the issue using some kind of self-contained gist, like this one that Rails uses for reporting ActiveStorage bugs? (but of course adding evil-seed to it)