This project is archived and is in readonly mode.
Serialize saves false boolean value as string "f" in sqlite3
Reported by Brian Underwood | November 28th, 2010 @ 07:45 PM | in 3.0.5
I notice that when I have a sample model such as this:
class Foo < ActiveRecord::Base
serialize :data
end
If I try to save the boolean value "true" to the "data" field, it works fine, but when I try to save the boolean value "false", it saves it as the string "f":
ruby-1.9.2-p0 > ActiveRecord::Base.logger = Logger.new(STDOUT)
...
ruby-1.9.2-p0 > f = Foo.first
Foo Load (0.2ms) SELECT "foos".* FROM "foos" LIMIT 1
=> #<Foo id: 1, data: true, created_at: "2010-11-28 19:11:05", updated_at: "2010-11-28 19:13:16">
ruby-1.9.2-p0 > f.data = false
=> false
ruby-1.9.2-p0 > f.save
AREL (0.5ms) UPDATE "foos" SET "data" = 'f', "updated_at" = '2010-11-28 19:25:02.547106' WHERE ("foos"."id" = 1)
It seems that this is an issue where type casting shouldn't apply to serialized fields. Could this be fixed simply? (I poked around in the Rails source for a bit without much luck on how to get started. Plus I'm not sure if this is limited to sqlite3 or if it's in other drivers).
Using Rails 3.0.3 and Ruby 1.9.2-p0 in RVM on Mac OS 10.6.5 (FWIW)
Comments and changes to this ticket
-
Aditya Sanghi November 29th, 2010 @ 11:32 AM
- State changed from new to open
- Importance changed from to Low
I can see this happen on 1.8.7. I also see that false gets serialized to 0 with mysql adapter.
-
Aditya Sanghi November 29th, 2010 @ 12:13 PM
On Rails master (will need to do work on Rails 3 stable branch a bit later) -
On Rails master, i see the behavior is that boolean false is actually saved as nil.
I think the problem is this line of code
if value && self.class.serialized_attributes.key?(name) value = YAML.dump value end
This makes sure that "values" that are set to false are never really YAML.dumped.
Will work on patch and failing test.
-
Aditya Sanghi November 29th, 2010 @ 12:46 PM
- Milestone set to 3.x
- Assigned user set to José Valim
- Tag changed from activerecord rails3, serialize, sqlite3 to activerecord rails3, patch, serialize, sqlite3
- Importance changed from Low to Medium
Adding patch for master to deal with attributes assigned as false to be serialized properly.
Now working on 3.0.x branch.
-
José Valim November 29th, 2010 @ 12:59 PM
- Assigned user changed from José Valim to Aaron Patterson
-
Aditya Sanghi November 29th, 2010 @ 01:00 PM
- Milestone cleared.
- Assigned user changed from Aaron Patterson to José Valim
And adding patch for 3-0 stable as well.
-
Aditya Sanghi November 29th, 2010 @ 01:00 PM
- Assigned user changed from José Valim to Aaron Patterson
-
Repository December 8th, 2010 @ 11:56 PM
- State changed from open to resolved
(from [97e9d8860b277e5ca9f4187c213ddd35d7f76e9b]) Ensure that boolean false is properly serialized [#6079 state:resolved] https://github.com/rails/rails/commit/97e9d8860b277e5ca9f4187c213dd...
-
Repository December 8th, 2010 @ 11:56 PM
(from [7a237d56aa38c51987af0fd52d001989c4f3da07]) Ensure that boolean false is properly serialized [#6079 state:resolved] https://github.com/rails/rails/commit/7a237d56aa38c51987af0fd52d001...
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile »
<h2 style="font-size: 14px">Tickets have moved to Github</h2>
The new ticket tracker is available at <a href="https://github.com/rails/rails/issues">https://github.com/rails/rails/issues</a>
People watching this ticket
Attachments
Referenced by
- 6079 Serialize saves false boolean value as string "f" in sqlite3 (from [97e9d8860b277e5ca9f4187c213ddd35d7f76e9b]) Ensure ...
- 6079 Serialize saves false boolean value as string "f" in sqlite3 (from [7a237d56aa38c51987af0fd52d001989c4f3da07]) Ensure ...