在 rails 7 中,我使用 gem 'activerecord-import' 將資料匯入我的資料庫并避免多次插入,例如:
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
當我運行代碼時:
Book.import my_books
我收到以下錯誤:
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=[]
發生該錯誤是因為 Rails 7 從 ActiveSupport::Callbacks::Callback 中洗掉了 raw_filter 方法,這會在呼叫 import 時觸發未定義的方法錯誤
如果您能幫我解決這個錯誤,我將不勝感激。
uj5u.com熱心網友回復:
看起來這個問題已經在 gem 中修復了,但更新版本還沒有發布。
我認為您基本上有兩種選擇:
- 等待更新版本發布。如果您能提供幫助,也許可以詢問維護者。或者
- 您可能希望直接
master從 GitHub的分支而不是 Rubygems安裝最新版本。但請記住,當正式發布完成后,您當然希望切換回 Rubygems 版本。
如果您想從 GitHub 安裝 gem,只需將帶有 gem 的行更新Gemfile為:
gem 'activerecord-import', github: 'zdennis/activerecord-import'
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/338272.html
