views:

883

answers:

2

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?

A: 

The problem may be with the database migration. I don't think that :bool is the right data type name to use. Try :boolean instead, e.g.

t.column :admin, :boolean, :default => false, :null => false
Schrockwell
+5  A: 

Use this instead:

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

Read why here.

drizzle
yeah, i figured it was something odd like that. Thanks :) +1 and marked as answered
Matt Briggs