Comments (2)
Yeah, I can see how this is irritating, but the behavior is actually pretty consistent. In the spec below, Active Record, Arel, and Baby Squeel all behave exactly the same way. As soon as you say as
, all type information is lost, because they think multiWordBool
is a totally different column.
require 'bundler/inline'
require 'minitest/spec'
require 'minitest/autorun'
gemfile true do
source 'https://rubygems.org'
gem 'activerecord'
gem 'sqlite3'
gem 'baby_squeel'
end
ActiveRecord::Base.establish_connection(adapter: 'sqlite3', database: ':memory:')
ActiveRecord::Schema.define do
create_table :users, force: true do |t|
t.boolean :multi_word_bool, null: false, default: true
end
end
class User < ActiveRecord::Base
end
class BabySqueelTest < Minitest::Spec
before { User.create! }
# Passes
it 'works without `as` in Baby Squeel' do
assert_equal true, User.selecting { multi_word_bool }.as_json.first['multi_word_bool']
end
it 'works without `as` in Active Record' do
assert_equal true, User.select('multi_word_bool').as_json.first['multi_word_bool']
end
it 'works without `as` in Arel' do
column = User.arel_table[:multi_word_bool]
assert_equal true, User.select(column).as_json.first['multi_word_bool']
end
# Fails
it 'fails in Baby Squeel' do
assert_equal true, User.selecting { multi_word_bool.as('multiWordBool') }.as_json.first['multiWordBool']
end
it 'fails in Active Record' do
assert_equal true, User.select('multi_word_bool as multiWordBool').as_json.first['multiWordBool']
end
it 'fails when using Arel' do
column = User.arel_table[:multi_word_bool].as('multiWordBool')
assert_equal true, User.select(column).as_json.first['multiWordBool']
end
end
Luckily, Active Record 5 has a very elegant solution for your specific problem: register a type. By adding the line below, all of the test above will pass.
class User < ActiveRecord::Base
attribute :multiWordBool, ActiveRecord::Type::Boolean.new
end
from baby_squeel.
Guess I should have done a bit more investigating. Thanks! I had not even thought about using a type like that, that looks great.
from baby_squeel.
Related Issues (20)
- Broken image in README.md HOT 2
- Breaks with Rails 6.0.0 HOT 28
- Breaks all Rails 5 has_many through relationship HOT 2
- Wrong table name when joining a polymorphic table twice HOT 2
- left_joins performs INNER JOIN with ActiveRecord 6.1.4.4 HOT 4
- Nested merge-joins query causes NoMethodError with ActiveRecord 6.1.4.4 HOT 3
- The 5.2 Builds are failing with the latest ransack version. HOT 1
- subqueries comparing with same table impossible HOT 1
- Jsonb Selectors HOT 2
- Support Arel.star HOT 1
- Reordering without explicit direction doesn't work with last in Rails 4 HOT 1
- Rails 5.2 support HOT 5
- Reference parent columns from subquery HOT 3
- Arel Deprecation Warning with Rails 5.2 HOT 2
- Intelligent Update queries
- Support `.nil?` in DSL HOT 4
- Sifters don't work on on-the-fly tables
- Gem breaking with Rails 5.2.1 HOT 1
- Breaks with Rails 5.2.1 HOT 57
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 baby_squeel.