user3399101
user3399101

Reputation: 1537

Setting boolean values in the DB

The way that boolean values are stored varies from database to database. Some use 1 and 0 integer values to represent true and false, respectively. Others use characters such as T and F.

How do I choose which to use? 1 and 0, or T and F? And likewise, how do I set them?

Please help clear this up for me!

Thanks!!

Upvotes: 2

Views: 150

Answers (2)

MrYoshiji
MrYoshiji

Reputation: 54882

Don't try to do the work of the DB adapters: depending on which you've set (one for MySQL, one for PostgreSQL, etc.), they will know what to write.

User.create(is_admin: false) # use Ruby's boolean classes, 
# the adapter will translate it in its own language to persist it in the DB

In the same way, the reverse will work perfectly:

User.first.is_admin # => return `true` or `false` (objects of Ruby, not a string like 'true' or 1 or 'T' or whatever)

Upvotes: 3

Babar
Babar

Reputation: 1202

Use true or false for Boolean, DB adapters handles the rest, in setting just mention if @model_object is an object of model then you can sat it like this
@model_object.attribute = false or true

Upvotes: -1

Related Questions