Skip to content

Instantly share code, notes, and snippets.

@claudiob
Last active January 11, 2016 15:52
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 claudiob/ae3452eaa3cf6307453a to your computer and use it in GitHub Desktop.
Save claudiob/ae3452eaa3cf6307453a to your computer and use it in GitHub Desktop.
STI + enum fail in Rails 5.0.0.beta1
begin
require 'bundler/inline'
rescue LoadError => e
$stderr.puts 'Bundler version 1.10 or later is required. Please update your Bundler'
raise e
end
gemfile(true) do
source 'https://rubygems.org'
gem 'rails', '>= 5.0.0.beta1', '< 5.1'
# gem 'rails' ## use this instead to test against the current Rails (4.2)
gem 'sqlite3'
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 :activities, force: true do |t|
t.string :type
t.integer :status, default: 0, null: false
end
end
class Activity < ActiveRecord::Base
enum status: %i(pending failed successful)
end
class Snapshot < Activity
end
class Adjustment < Activity
end
class BugTest < Minitest::Test
def test_sti_enum
Adjustment.create! status: 'successful'
Snapshot.create! status: 'successful'
# The following passes both on Rails 4.2 and on Rails 5.0.0.beta1.
assert Snapshot.first.is_a?(Snapshot)
# The following fails only on Rails 5.0.0.beta1 (returns an Adjustment).
assert Snapshot.successful.first.is_a?(Snapshot)
end
end
@claudiob
Copy link
Author

When run against Rails 4.2, these are the SQL queries I see in the log file:

# assert Snapshot.first.is_a?(Snapshot)
SELECT  "activities".* FROM "activities" WHERE "activities"."type" IN ('Snapshot')  ORDER BY "activities"."id" ASC LIMIT 1

# assert Snapshot.successful.first.is_a?(Snapshot)
SELECT  "activities".* FROM "activities" WHERE "activities"."type" IN ('Snapshot') AND "activities"."status" = ?  ORDER BY "activities"."id" ASC LIMIT 1  [["status", 2]]

However, when run against Rails 5.0.0.beta1, these are the SQL queries I see in the log file:

# assert Snapshot.first.is_a?(Snapshot)
SELECT  "activities".* FROM "activities" WHERE "activities"."type" IN ('Snapshot') ORDER BY "activities"."id" ASC LIMIT ?  [["LIMIT", 1]]

# assert Snapshot.successful.first.is_a?(Snapshot)
SELECT  "activities".* FROM "activities" WHERE "activities"."status" = ? ORDER BY "activities"."id" ASC LIMIT ?  [["status", 2], ["LIMIT", 1]]

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment