ActiveRecord & Sqlite3 & serialize & "f"

What's wrong with the code below?

It returns this "f" instead of "". Where does "f" come from?

Thanks.

gegroet,
Erik V. - http://www.erikveen.dds.nl/

···

----------------------------------------------------------------

$ ruby test.rb
:bar
"f" <<<< Should be ""
nil

$ echo "select * from key_values;" | sqlite3 test.db
1|--- :foo
>--- :bar

2|--- :empty
>f
3|--- :nil
>

----------------------------------------------------------------

require "rubygems"
require "active_record"

class KeyValue < ActiveRecord::Base
   serialize :key
   serialize :value

   def self.create_table
     unless connection.tables.include?(table_name)
       connection.create_table(table_name) do |table|
         table.column :id , :primary_key
         table.column :key , :text
         table.column :value , :text
       end
     end
   end

   def self.[](key)
     find_or_create_by_key(key).value
   end

   def self.[]=(key, value)
     config = find_or_create_by_key(key)
     config.value = value
     config.save
   end
end

File.delete("test.db") if File.file?("test.db")

ActiveRecord::Base.establish_connection(:adapter=>"sqlite3", :database=>"test.db")

KeyValue.create_table

KeyValue[:foo] = :bar
KeyValue[:empty] = ""
KeyValue[:nil] = nil

p KeyValue[:foo]
p KeyValue[:empty]
p KeyValue[:nil]

----------------------------------------------------------------

sqlite3 returns #f for null columns. Google "sqlite3 null f" for some
references. Somewhere in the confusion it seems your "" is becoming a null
in the database.

Cheers,
Peter Cooper

···

On 7/20/07, Erik Veenstra <erikveen@gmail.com> wrote:

What's wrong with the code below?

It returns this "f" instead of "". Where does "f" come from?