升级到Rails 4.2后,将上传的文件保存到数据库( pg )的代码失败,并显示错误消息:'string contains null byte'。
我创建了一个只有一个模型和一个二进制字段的新Rails 4.2应用程序。
create_table "entries", force: :cascade do |t|
t.string "name"
t.binary "file"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
end
使用控制台:
:001 > entry = Entry.new
=> #<Entry id: nil, name: nil, file: nil, created_at: nil, updated_at: nil>
:002 > f = File.open('public/icon-sidesearch.png', 'rb') { |io| io.read }
=> "\x89PNG\r\n\u001A\n\u0000\... more binary data"
:003 > entry.file = f
ArgumentError: string contains null byte
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/oid/bytea.rb:8:in `unescape_bytea'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/connection_adapters/postgresql/oid/bytea.rb:8:in `type_cast_from_database'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/type/binary.rb:26:in `changed_in_place?'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute.rb:54:in `changed_in_place_from?'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:74:in `attribute_changed_in_place?'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:149:in `block in changed_in_place'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:148:in `select'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:148:in `changed_in_place'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:141:in `attributes_changed_in_place'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:62:in `changed_attributes'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activemodel-4.2.0/lib/active_model/dirty.rb:173:in `attribute_changed?'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:107:in `save_changed_attribute'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods/dirty.rb:94:in `write_attribute'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activerecord-4.2.0/lib/active_record/attribute_methods.rb:50:in `__temp__6696c656'
from (irb):5
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/railties-4.2.0/lib/rails/commands/console.rb:110:in `start'
... 9 levels...
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `block in load'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:240:in `load_dependency'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/activesupport-4.2.0/lib/active_support/dependencies.rb:268:in `load'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/spring-1.2.0/lib/spring/commands/rails.rb:6:in `call'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/spring-1.2.0/lib/spring/command_wrapper.rb:38:in `call'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:183:in `block in serve'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:156:in `fork'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:156:in `serve'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:131:in `block in run'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:125:in `loop'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/spring-1.2.0/lib/spring/application.rb:125:in `run'
from /Users/jose/.rvm/gems/ruby-2.1.2@rails42_sice/gems/spring-1.2.0/lib/spring/application/boot.rb:18:in `<top (required)>'
from /Users/jose/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from /Users/jose/.rvm/rubies/ruby-2.1.2/lib/ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from -e:1:in `<main>'
任何想法可能会发生什么?