我正在尝试使用 activerecord-import gem,并且我已按照 wiki 上的说明进行操作,但我得到了 NoMethodError: undefined method 'import' for #<Class:0x8b009b0>
。这是我的代码(与 wiki 中的示例基本相同)
class ExampleCode
def self.testing
orders = []
10.times do |i|
orders << Order.new(:raw_data => "order #{i}")
end
Order.import orders
end
end
我这样调用该方法:
ExampleCode.testing
我已经在 Windows、Linux、sqlite 数据库、mysql 数据库上尝试过,但仍然没有成功。我确信我已经安装了 gem:
actionmailer (3.2.6, 3.2.3, 3.2.1, 3.2.0)
actionpack (3.2.6, 3.2.3, 3.2.1, 3.2.0)
activemodel (3.2.6, 3.2.3, 3.2.1, 3.2.0)
activerecord (3.2.6, 3.2.3, 3.2.1, 3.2.0)
activerecord-import (0.2.10)
activerecord-oracle_enhanced-adapter (1.4.1)
activerecord-sqlserver-adapter (3.2.1)....
我什至尝试使用 require (安装 gem 时这不应该是必需的。我没有在其他地方看到过这种情况,所以我担心我必须错过一些非常明显的东西
最佳答案
您必须导入 active_record 和 activerecord-import
即
require active_record
require activerecord-import
(如维基百科中所述)
原因是,除非您显式导入这些库,否则 ruby 不会知道这一点。如果是 Rails 项目,rails 会为您导入 Gemfile 中提到的所有 Gem。
关于ruby-on-rails - activerecord-import gem NoMethodError : undefined method 'import' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12187813/