Skip to content

Instantly share code, notes, and snippets.

@alpaca-tc
Created October 2, 2020 09:36
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alpaca-tc/e6c374be0d077966e4e2d1e5dbb43898 to your computer and use it in GitHub Desktop.
Save alpaca-tc/e6c374be0d077966e4e2d1e5dbb43898 to your computer and use it in GitHub Desktop.
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", '~> 6.0.0'
gem "sqlite3"
gem "pry"
end
require "pry"
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 :users, force: true do |t|
t.string :name, null: false
end
create_table :shops, force: true do |t|
t.references :user, null: false
end
create_table :items, force: true do |t|
t.references :shop, null: false
t.integer :state, null: false
end
end
class User < ActiveRecord::Base
has_one :shop, autosave: true
end
class Shop < ActiveRecord::Base
belongs_to :user
has_many :items
end
class Item < ActiveRecord::Base
belongs_to :shop
enum(state: %i[draft published])
end
class BugTest < Minitest::Test
# expected nothing raised, got ActiveRecord::ReadOnlyRecord
def test_join_and_enum
assert_match /WHERE "items"."state" = 0/, User.joins(shop: :items).merge(Item.where(state: ['draft'])).to_sql
# expected WHERE "items"."state" = 0, got WHERE "items"."state" = NULL
assert_match /WHERE "items"."state" = 0/, User.joins(shop: :items).where(items: { state: ['draft'] }).to_sql
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment