Reputation: 2476
I've added a boolean column to my active record migration
class AddIncludeInConsolidationToCompanies < ActiveRecord::Migration
def change
add_column :companies, :include_in_consolidation, :bool, :default => true
end
end
Whenever I fetch a record from the database I get "f" or "t" instead of true or false.
Is activerecord not supposed to automatically handle the type casting to and from the database.
It's like ActiveRecord::Base.connection.quoted_true/false
are defaulting to true.
What is the best way around this? Ideally it should just work, a boolean column should return a boolean by default not a string.
Upvotes: 3
Views: 2654
Reputation: 1
While boolean values are output using t and f in postgresql, "The key words TRUE and FALSE are the preferred (SQL-compliant) usage:"
http://www.postgresql.org/docs/9.4/static/datatype-boolean.html
Not sure why this choice was made.
Upvotes: 0
Reputation: 3212
't' and 'f' are what PostgreSQL uses for boolean values.
'bool' is not a valid datatype for a Rails migration. You need to use 'boolean'. My guess is that 'bool' is getting the column created in the database, but Rails is confused when the data is loaded into a model. Change and rerun the migration and I bet everything works out.
Upvotes: 4
Reputation: 1845
May be I'm wrong, but the booleans are saved as 'f' or 't' in PostgreSQL.
But you can use the traditional true/false in your RoR logic. Given a User model with a boolean attribute named "active" you can do
if !user.active?
# do something
else
# do something else
end
As you can see the true/false test works fine...
Cheers
Upvotes: 0