Giter VIP home page Giter VIP logo

Comments (2)

rzane avatar rzane commented on July 16, 2024 1

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.

supremebeing7 avatar supremebeing7 commented on July 16, 2024 1

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.