Lighthouse has a new layout. Prefer the old one? Return to the old layout, and switch back any time from the link at the top of each page.

This project is archived and is in readonly mode.

Serialize saves false boolean value as string "f" in sqlite3

#6079

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)

Reported by Brian Underwood · November 28th, 2010 @ 07:45 PM

State: resolved
Milestone: 3.0.5
Assigned to: Aaron Patterson Aaron Patterson
Importance: Medium

Activity

  1. Aditya Sanghi
    Aditya Sanghi
    • 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.

    November 29th, 2010 @ 11:32 AM

  2. Aditya Sanghi
    Aditya Sanghi

    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.

    November 29th, 2010 @ 12:13 PM

  3. Aditya Sanghi
    Aditya Sanghi
    • 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.

    November 29th, 2010 @ 12:46 PM

  4. José Valim
    José Valim
    • Assigned user changed from José Valim to Aaron Patterson

    November 29th, 2010 @ 12:59 PM

  5. Aditya Sanghi
    Aditya Sanghi
    • Milestone cleared.
    • Assigned user changed from Aaron Patterson to José Valim

    And adding patch for 3-0 stable as well.

    November 29th, 2010 @ 01:00 PM

  6. Aditya Sanghi
    Aditya Sanghi
    • Assigned user changed from José Valim to Aaron Patterson

    November 29th, 2010 @ 01:00 PM

  7. Repository
  8. Repository
  9. Santiago Pastorino