Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ActiveRecord boolean value returning "f", "t"

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.

like image 301
Martinffx Avatar asked May 01 '26 08:05

Martinffx


1 Answers

'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.

like image 80
Nick Messick Avatar answered May 05 '26 13:05

Nick Messick