Comments (5)
I'd say ideally it should be fixed, but I fear this may break apps, and I'm not convinced this is a big enough problem to take that risk.
If you have time it would be worth to use git blame to find the commit that introduced this behavior to see if there was any historical reason to do so.
from rails.
Some databases didn't had boolean, so you needed to transform them in integer. That is the reason for this behavior.
SQLite is one of them
2.1. Boolean Datatype
SQLite does not have a separate Boolean storage class. Instead, Boolean values are stored as integers 0 (false) and 1 (true).
SQLite recognizes the keywords "TRUE" and "FALSE", as of version 3.23.0 (2018-04-02) but those keywords are really just alternative spellings for the integer literals 1 and 0 respectively.
https://www.sqlite.org/datatype3.html
from rails.
Some databases didn't had boolean, so you needed to transform them in integer. That is the reason for this behavior.
@rafaelfranca, I believe the issue lies in how the find
, find_by_id
, and find_by(id: ...)
methods filter based on the primary key id, typically an integer.
The complication arises with databases like SQLite, lacking a distinct Boolean storage class. Consequently, we're converting Boolean values to integers.
When passing a Boolean to these methods, incorrect results occur:
True returns records with id equal to 1.
False returns records with id equal to 0.
Indeed, the desired behavior would be for the methods to return nil when passed a Boolean value. However, currently, they incorrectly return records with id equal to 1 for True and 0 for False
from rails.
@olepalm I believe this behavior is expected since there are test cases for it in ActiveModel Integer, where we serialize boolean values to integers.
rails/activemodel/test/cases/type/integer_test.rb
Lines 41 to 45 in 343e781
We need to await comments from the Rails core team regarding whether this behavior is expected or necessitates a fix. If a fix is indeed required, then we'll need to update the serialize method accordingly.
I'll submit a pull request once we receive confirmation from the Rails maintenance team.
rails/activemodel/lib/active_model/type/integer.rb
Lines 65 to 68 in 343e781
to
def serialize(value)
return if value.is_a?(::TrueClass) || value.is_a?(::FalseClass)
return if value.is_a?(::String) && non_numeric_string?(value)
ensure_in_range(super)
end
Reproduce Script:
# frozen_string_literal: true
require "bundler/inline"
require "debug"
gemfile(true) do
source "https://rubygems.org"
# If you want to test against edge Rails replace the previous line with this:
gem "rails", github: "rails/rails", branch: "main"
gem "sqlite3", "~> 1.4"
end
require "active_record"
require "minitest/autorun"
require "logger"
# This connection will do for database-independent bug reports.
ActiveRecord::Base.establish_connection(adapter: "sqlite3", database: ":memory:")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts do |t|
t.string :title
t.timestamps
end
end
class Post < ActiveRecord::Base
end
class BugTest < Minitest::Test
def test_find_methods
post = Post.new(title: "Example Post")
post.save
post = Post.last
assert_nil Post.find_by(id: true)
assert_nil Post.find_by_id(true)
assert_raises do
Post.find(true)
end
end
end
from rails.
@byroot / @fatkodima Does this need to be fixed, or is it the desired behavior?
from rails.
Related Issues (20)
- Possible race condition in postgresql type_map initialization
- rspec tests won't after upgrading from rails 7.1.2 to 7.1.3.2: can't modify frozen Array when requiring environment file HOT 2
- form_with reverting to HTML processing only HOT 5
- Inaccurate docs/comments about ActiveStorage video previewer defaults could be improved HOT 4
- Inconsistent model state when dumped and loaded with Marshal version 7.1 HOT 4
- ActiveRecord connection is checked out/retained despite setting `config.active_record.permanent_connection_checkout = :disallowed` HOT 4
- Default Active Support Logger in Addition to Custom Logger in Rails 7.1 x BroadcastLogger Class in Development environment HOT 1
- Aws::Rails::SqsActiveJob#execute captures StandardError which is too wide HOT 1
- AppGeneratorTest fails because `Could not find gem 'kamal' in locally installed gems.` HOT 1
- `has_one :through` associations ignore preloaded records
- loading credentials key HOT 5
- Different timestamps behaviour with InsertAll HOT 3
- Activerecord: Insert fails on composed_primary_key model with id as not null Identity column
- procfile causes failure on Rails 8.0 HOT 8
- Rails 7.1 changes established database connection after calling `rails db:test:prepare` for multi-database apps HOT 4
- Storage folder seems like it is sometimes not needed
- Storage folder sometimes doesn't need Kamal configuration in Rails 8.0.0.alpha HOT 1
- Content-Type parsing breaks on valid test cases HOT 4
- PostgreSQL Apache AGE Rails (GraphDB driver questions / problems) HOT 6
- s
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from rails.