Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

Add check if value is frozen before attempting to mutate via encode! #5209

Closed
wants to merge 1 commit into from

4 participants

@losvedir

This fixes the following error I was receiving (the "day" string, e.g., "Thursday" was frozen by rails, presumably because it's Encoding::ASCII_8BIT? Therefore, when it gets "encode!"-ed to utf-8, a RuntimeError occurs.). This is related to this stackoverflow question (http://stackoverflow.com/questions/9474730/rails-active-record-runtimeerror-cant-modify-frozen-string-somehow-related).

(0.1ms) begin transaction
Binary data inserted for string type on column day
SQL (0.5ms) INSERT INTO "availabilities" ("coach_id", "created_at", "day", "hour", "updated_at") VALUES (?, ?, ?, ?, ?) [["coach_id", 3], ["created_at", Tue, 28 Feb 2012 18:05:33 UTC +00:00], ["day", "Monday"], ["hour", 18], ["updated_at", Tue, 28 Feb 2012 18:05:33 UTC +00:00]]
RuntimeError: can't modify frozen String: INSERT INTO "availabilities" ("coach_id", "created_at", "day", "hour", "updated_at") VALUES (?, ?, ?, ?, ?)
(0.1ms) rollback transaction
Completed 500 Internal Server Error in 59ms

@losvedir losvedir Add check to see if value is frozen before attempting to mutate via e…
…ncode!


This fixes the following error I was receiving:
(0.1ms)  begin transaction
Binary data inserted for `string` type on column `day`
  SQL (0.5ms)  INSERT INTO "availabilities" ("coach_id", "created_at", "day", "hour", "updated_at") VALUES (?, ?, ?, ?, ?)  [["coach_id", 3], ["created_at", Tue, 28 Feb 2012 18:05:33 UTC +00:00], ["day", "Monday"], ["hour", 18], ["updated_at", Tue, 28 Feb 2012 18:05:33 UTC +00:00]]
RuntimeError: can't modify frozen String: INSERT INTO "availabilities" ("coach_id", "created_at", "day", "hour", "updated_at") VALUES (?, ?, ?, ?, ?)
   (0.1ms)  rollback transaction
Completed 500 Internal Server Error in 59ms
36f3cb1
@losvedir

The reason a value might be frozen is if it's serving as the key of a hash table.

@losvedir losvedir closed this
@losvedir losvedir reopened this
@losvedir

Meant to comment, not close the request...

@lest

Would you mind writing a test case to make sure it won't be broken in future?

@losvedir

Yes, I will write up a test case. Heh, yes... should be 'else'. All the more reason for said test case!

@carlosantoniodasilva

@losvedir thanks for your pull request. Sqlite adapter has been removed from Rails master, so if you want to try out and send a new pull request with tests for sqlite3 adapter, in case you're still seeing this issue, please go ahead. I'm closing here, thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Feb 28, 2012
  1. @losvedir

    Add check to see if value is frozen before attempting to mutate via e…

    losvedir authored
    …ncode!
    
    
    This fixes the following error I was receiving:
    (0.1ms)  begin transaction
    Binary data inserted for `string` type on column `day`
      SQL (0.5ms)  INSERT INTO "availabilities" ("coach_id", "created_at", "day", "hour", "updated_at") VALUES (?, ?, ?, ?, ?)  [["coach_id", 3], ["created_at", Tue, 28 Feb 2012 18:05:33 UTC +00:00], ["day", "Monday"], ["hour", 18], ["updated_at", Tue, 28 Feb 2012 18:05:33 UTC +00:00]]
    RuntimeError: can't modify frozen String: INSERT INTO "availabilities" ("coach_id", "created_at", "day", "hour", "updated_at") VALUES (?, ?, ?, ?, ?)
       (0.1ms)  rollback transaction
    Completed 500 Internal Server Error in 59ms
This page is out of date. Refresh to see the latest.
View
6 activerecord/lib/active_record/connection_adapters/sqlite_adapter.rb
@@ -203,7 +203,11 @@ def type_cast(value, column) # :nodoc:
value = super
if column.type == :string && value.encoding == Encoding::ASCII_8BIT
@logger.error "Binary data inserted for `string` type on column `#{column.name}`"
- value.encode! 'utf-8'
+ if value.frozen?
+ value = value.dup.encode! 'utf-8'
+ elsif
+ value.encode! 'utf-8'
+ end
end
value
end
Something went wrong with that request. Please try again.