ruby-on-railsrubyactiverecordrubygemsactivesupport

How to add raw_filter method to ruby ​on rails 7?


In rails 7, I'm using the gem 'activerecord-import' to import data to my database and avoid multiple inserts, for example:

  my_books = []
  10.times do |i|
    my_books << Book.new(name: "book #{i}")
  end

  # In the line below the error appears
  Book.import my_books

When I ran the code:

Book.import my_books

I get the following error:

undefined method `raw_filter' for #<ActiveSupport::Callbacks::Callback:0x000055e21adb4ed8 @chain_config={:scope=>:name, :terminator=>#<Proc:0x000055e21ac126e8 /usr/local/bundle/bundler/gems/rails-2432988f9fc0/activesupport/lib/active_support/callbacks.rb:589>}, @name=:validate, @kind=:before, @filter=:cant_modify_encrypted_attributes_when_frozen, @if=[#<Proc:0x000055e21adb5428 /usr/local/bundle/bundler/gems/rails-2432988f9fc0/activerecord/lib/active_record/encryption/encryptable_record.rb:13 (lambda)>], @unless=[]

That error occurs because Rails 7 removes the raw_filter method from the ActiveSupport:: Callbacks::Callback which triggers an undefined method error when calling import

If you could help me solve this error, I'd appreciate it very much.


Solution

  • It looks like this issue has already been fixed in the gem but the updated version hasn't been released yet.

    I think you basically have two options:

    1. Wait for the updated version to be released. Perhaps ask the maintainer if you can help. Or
    2. You might want to install the latest version directly from the master branch from GitHub instead of from Rubygems. But keep in mind that you certainly want to switch back to the Rubygems version when the official release was done.

    If you want to install the gem from GitHub just update the line with the gem in your Gemfile to:

    gem 'activerecord-import', github: 'zdennis/activerecord-import'