Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add three-state boolean problem #275

Merged
merged 1 commit into from Dec 7, 2020

Conversation

tejasbubane
Copy link
Contributor

README.adoc Outdated Show resolved Hide resolved
README.adoc Outdated Show resolved Hide resolved
README.adoc Outdated Show resolved Hide resolved
Copy link
Member

@pirj pirj left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you!

Added a couple of cosmetic notes.

README.adoc Outdated Show resolved Hide resolved
README.adoc Outdated Show resolved Hide resolved
README.adoc Outdated Show resolved Hide resolved
@tejasbubane
Copy link
Contributor Author

Thanks @pirj I have made the suggested changes.

README.adoc Outdated Show resolved Hide resolved
README.adoc Outdated
With SQL databases, if a boolean column is not given a default value, it ends up having three possible values: `true`, `false` and `NULL`.
Boolean operators [work in unexpected ways](https://en.wikipedia.org/wiki/Three-valued_logic) with `NULL`.

E.g. in SQL queries, `true AND NULL` is `NULL` (not false), `true AND NULL OR false` is `NULL` (not false). This can make SQL queries return unexpected results.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Minor, but starting a sentence with "E.g." feels awkward. How about "For example"?

README.adoc Outdated
@@ -1026,6 +1026,24 @@ end
While enforcing table defaults only in Rails is suggested by many Rails developers, it's an extremely brittle approach that leaves your data vulnerable to many application bugs.
And you'll have to consider the fact that most non-trivial apps share a database with other applications, so imposing data integrity from the Rails app is impossible.

=== 3-state Boolean [[three-state-boolean]]

With SQL databases, if a boolean column is not given a default value, it ends up having three possible values: `true`, `false` and `NULL`.
Copy link
Contributor

@andyw8 andyw8 Dec 3, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think "will have" reads better than "ends up having".

README.adoc Outdated

E.g. in SQL queries, `true AND NULL` is `NULL` (not false), `true AND NULL OR false` is `NULL` (not false). This can make SQL queries return unexpected results.

To avoid such situations, boolean columns should always have a default value and `NOT NULL` constraint.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

"and a NOT NULL"

add_column :users, :active, :boolean

# good - boolean with a default value and with restricted NULL
add_column :users, :active, :boolean, default: true, null: false
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd prefer an example code of the rule that defaults to false here :-) Of course, In practice, false or true is chosen depending on the context.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Or I think one approach is to exemplify an example with default: false and an example with default: true.

README.adoc Outdated
# bad - boolean without a default value
add_column :users, :active, :boolean

# good - boolean with a default value and with restricted NULL
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
# good - boolean with a default value and with restricted NULL
# good - boolean with a default value (`false` or `true`) and with restricted NULL

@tejasbubane
Copy link
Contributor Author

@andyw8 @koic Thanks. I have made the suggested changes.

@koic koic merged commit cd6da97 into rubocop:master Dec 7, 2020
@koic
Copy link
Member

koic commented Dec 7, 2020

Thanks!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants