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

Type cast falsy boolean symbols on boolean attribute as false #35794

Merged
merged 1 commit into from Mar 29, 2019

Conversation

@kamipo
Copy link
Member

@kamipo kamipo commented Mar 29, 2019

Before 34cc301, type casting by boolean attribute when querying is a
no-op, so finding by truthy boolean string (i.e.
where(value: "true") # => value = 'true') didn't work as expected
(matches it to FALSE in MySQL #32624). By type casting is ensured, a
value on boolean attribute is always serialized to TRUE or FALSE.

In PostgreSQL, where(value: :false) # => value = 'false' was a valid
SQL, so 34cc301 is a regresson for PostgreSQL since all symbol values
are serialized as TRUE.

I'd say using :false is mostly a developer's mistake (user's input
basically comes as a string), but :false on boolean attribute is
serialized as TRUE is not a desirable behavior for anybody.

This allows falsy boolean symbols as false, i.e.
klass.create(value: :false).value? # => false and
where(value: :false) # => value = FALSE.

Fixes #35676.

Before 34cc301, type casting by boolean attribute when querying is a
no-op, so finding by truthy boolean string (i.e.
`where(value: "true") # => value = 'true'`) didn't work as expected
(matches it to FALSE in MySQL rails#32624). By type casting is ensured, a
value on boolean attribute is always serialized to TRUE or FALSE.

In PostgreSQL, `where(value: :false) # => value = 'false'` was a valid
SQL, so 34cc301 is a regresson for PostgreSQL since all symbol values
are serialized as TRUE.

I'd say using `:false` is mostly a developer's mistake (user's input
basically comes as a string), but `:false` on boolean attribute is
serialized as TRUE is not a desirable behavior for anybody.

This allows falsy boolean symbols as false, i.e.
`klass.create(value: :false).value? # => false` and
`where(value: :false) # => value = FALSE`.

Fixes rails#35676.
@kamipo
Copy link
Member Author

@kamipo kamipo commented Mar 29, 2019

Should this be backported to 5-2-stable?
This fixes #35676, but also changes the behavior of casting klass.create(value: :false).value? from true to false.

@rafaelfranca
Copy link
Member

@rafaelfranca rafaelfranca commented Mar 29, 2019

Was it false before 5.2.3 or true?

@kamipo
Copy link
Member Author

@kamipo kamipo commented Mar 29, 2019

klass.create(value: :false).value? is true since 5.0 (nil before 5.0).

@kamipo kamipo merged commit 406d3a9 into rails:master Mar 29, 2019
3 checks passed
@kamipo kamipo deleted the type_cast_symbol_false branch Mar 29, 2019
@rafaelfranca
Copy link
Member

@rafaelfranca rafaelfranca commented Mar 29, 2019

hmmm. Let's backport and see if people complain about it during the rc

kamipo added a commit that referenced this issue Mar 29, 2019
Type cast falsy boolean symbols on boolean attribute as false
@kamipo
Copy link
Member Author

@kamipo kamipo commented Mar 29, 2019

Sure, I've backported in 724e1f4.

suketa added a commit to suketa/rails_sandbox that referenced this issue Sep 28, 2019
Type cast falsy boolean symbols on boolean attribute as false
rails/rails#35794
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Linked issues

Successfully merging this pull request may close these issues.

None yet

2 participants