2

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!!

1
  • PostgreSQL happily accepts true/false, 0/1 or 't'/'f' as boolean literals (per the documentation). That said, as @MrYoshiji says, with Rails the ORM takes care of this detail for you. Commented May 14, 2014 at 7:09

2 Answers 2

3

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)
Sign up to request clarification or add additional context in comments.

Comments

-1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.