红宝石。将从NET-SFTP pull创建的数组数据保存到使用ActiveRecord的数据库时的NoMethodError

时间:2012-08-24 18:12:49

标签: ruby activerecord sqlite sftp net-sftp

我有一个小的ruby程序登录到sftp服务器,从服务器上的目录中提取几个文件名,然后将这些文件名写入sqlite3数据库。

我将文件名从sftp保存到数组中。我可以确认数组已创建并具有值,但是当我尝试使用该数组中的数据保存到数据库时,我收到错误,老实说,我无法弄清楚错误的含义是什么?或者为什么数组中的数据没有被写入?

另一个奇怪的事情是我可以手动创建一个具有与sftp完全相同的值的数组,并使用该新数组中的相同数据成功保存到数据库。为什么从sftp数据创建的数组会导致错误,但是我使用完全相同的数据手动创建的数组成功写入数据库?

感谢您的帮助!

我的程序(文件:xb.rb):

# db
require 'active_record'
ActiveRecord::Base.establish_connection(
  :adapter => 'sqlite3',
  :database => 'D:/prog/test/db.sqlite3.db'
)
class Db < ActiveRecord::Base
end

# connect to sftp and get last 2 filenames, put them in array
require 'net/sftp'
@files = []
Net::SFTP.start('XXX.XXX.XXX.XXX','xxx',:password => 'xxx') do |sftp|
    sftp.dir.foreach("/Outgoing/") do |sftp_file|
    @files.push(sftp_file.name)
  end
end

# Get last 2 filenames
@files = @files.sort!.pop(2)
# Show array to confirm it has values and that it's an array
puts @files
puts @files.class

# Write filenames to db
@files.each do |new_file|
  Db.create(:name => new_file)
end

返回:

PS D:\prog\test> ruby xb.rb
20120823002349.txt
20120824012407.txt
Array
D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/sqlite_adapter.rb:212:in `type_cast': NoMethodError: undefined method `error' for nil:NilClass: INSERT INTO "dbs" ("name") VALUES (?) (ActiveRecord::StatementInvalid)
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/sqlite_adapter.rb:264:in `block (2 levels) in exec_query'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/sqlite_adapter.rb:263:in `map'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/sqlite_adapter.rb:263:in `block in exec_query'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:280:in `block in log'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/notifications/instrumenter.rb:20:in `instrument'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract_adapter.rb:275:in `log'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/sqlite_adapter.rb:247:in `exec_query'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:61:in `exec_insert'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:88:in `insert'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/query_cache.rb:14:in `insert'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/relation.rb:66:in `insert'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/persistence.rb:363:in `create'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/timestamp.rb:57:in `create'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/callbacks.rb:268:in `block in create'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:403:in `_run__619764720__create__513830955__callbacks'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:405:in `__run_callback'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:385:in `_run_create_callbacks'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/callbacks.rb:268:in `create'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/persistence.rb:344:in `create_or_update'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/callbacks.rb:264:in `block in create_or_update'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:414:in `_run__619764720__save__513830955__callbacks'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:405:in `__run_callback'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:385:in `_run_save_callbacks'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activesupport-3.2.1/lib/active_support/callbacks.rb:81:in `run_callbacks'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/callbacks.rb:264:in `create_or_update'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/persistence.rb:84:in `save'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/validations.rb:50:in `save'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/attribute_methods/dirty.rb:22:in `save'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/transactions.rb:241:in `block (2 levels) in save'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/transactions.rb:295:in `block in with_transaction_returning_status'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/connection_adapters/abstract/database_statements.rb:190:in `transaction'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/transactions.rb:208:in `transaction'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/transactions.rb:293:in `with_transaction_returning_status'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/transactions.rb:241:in `block in save'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/transactions.rb:252:in `rollback_active_record_state!'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/transactions.rb:240:in `save'
        from D:/RailsInstaller/Ruby1.9.3/lib/ruby/gems/1.9.1/gems/activerecord-3.2.1/lib/active_record/persistence.rb:45:in `create'
        from xb.rb:27:in `block in <main>'
        from xb.rb:26:in `each'
        from xb.rb:26:in `<main>'

但是如果我手动创建一个与@files完全相同的数组(@ files2)(我将@ files2与@files进行比较以确认),我可以成功地使用@ files2中的数据写入数据库。

测试(附加到上一个程序):

.
.
.
# create manual array with same data as @files
@files2 = ["20120823002349.txt","20120824012407.txt"]
puts @files == @files2

# Write filenames to db
@files2.each do |new_file|
  Db.create(:name => new_file)
end

返回:

PS D:\prog\test> ruby xb.rb
20120823002349.txt
20120824012407.txt
Array
true

测试将数组值写入db。我很困惑,为什么我不能使用sftp数组(@files)中的数据写入db,当@ files2具有完全相同的数据并且可以用来写入db?

我感谢任何帮助,想法,想法。

0 个答案:

没有答案