Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

booleans in rails with sqlite

I am a bit of a noob still with rails, but I am running across something that seems a bit odd. I added a boolean field to a model in the database thusly

t.column :admin, :bool, :default => false, :null => false

However, the value in the sqlite3 database seems to be either 't' or 'f'. That is fine, but I would still expect user.admin? to return false if the value is 'f'. As you can see from the following console session, that's not the case:

>> user = User.first
=> #<User id: 2, login: "matt", name: "", email: "[email protected]", crypt
ed_password: "c6740f820b4cbf6e3d88188719f23cd3053a54f0", salt: "5629f5ee09f51543
a7d64dd903b8d9e53aa43a24", created_at: "2009-04-26 23:08:05", updated_at: "2009-
04-26 23:10:38", remember_token: nil, remember_token_expires_at: nil, admin: "t"
>
>> user.admin?
=> true
>> user.admin = false
=> false
>> user.save
=> true
>> user = User.first
=> #<User id: 2, login: "matt", name: "", email: "[email protected]", crypt
ed_password: "c6740f820b4cbf6e3d88188719f23cd3053a54f0", salt: "5629f5ee09f51543
a7d64dd903b8d9e53aa43a24", created_at: "2009-04-26 23:08:05", updated_at: "2009-
05-06 03:32:23", remember_token: nil, remember_token_expires_at: nil, admin: "f"
>
>> user.admin?
=> true

Is this just some weird issue with sqlite, or am I just not getting something?

like image 951
Matt Briggs Avatar asked May 06 '09 03:05

Matt Briggs


1 Answers

Use this instead:

t.column :admin, :boolean, :default => false, :null => false

Read why here.

like image 61
drizzle Avatar answered Sep 21 '22 23:09

drizzle