ActiveRecord & Sqlite3 & serialize & "f"

E

Erik Veenstra

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]

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

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,969
Messages
2,570,161
Members
46,705
Latest member
Stefkari24

Latest Threads

Top