After upgrading to Rails 4.2 my code to save an uploaded file to the database (pg) fails with the error message: 'string contains null byte'.
I've created a new Rails 4.2 app with just one model and a binary field.
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
Using the console:
: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>'
Any ideas what might be happening?
As mentioned here, that is a Rails bug and fixed on 4.2.1
https://github.com/rails/rails/pull/17680
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With