Skip to content

Instantly share code, notes, and snippets.

@klyonrad
Last active July 27, 2022 11:02
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 klyonrad/e2695c37c1e6d4c5a3cffc26f212b8ba to your computer and use it in GitHub Desktop.
Save klyonrad/e2695c37c1e6d4c5a3cffc26f212b8ba to your computer and use it in GitHub Desktop.
rails enum UnknownAttributeError
# frozen_string_literal: true
require "bundler/inline"
gemfile(true) do
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
# Activate the gem you are reporting the issue against.
gem "activerecord", "~> 7.0.0"
gem "sqlite3"
gem "mysql2"
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.establish_connection(adapter: "sqlite3", database: "rails_enum_bug.sqlite")
# ActiveRecord::Base.establish_connection(adapter: "mysql2", database: "rails_enum_bug_report", username: 'root', host: '127.0.0.1')
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
end
end
class Post < ActiveRecord::Base
enum :wrong_attributee, [:default_value, :middle_value, :other_value]
end
class AddAttributeToPosts < ActiveRecord::Migration[7.0]
def change
add_column :posts, :wrong_attrib, :integer, default: 0
end
end
class AddEnumToPosts < ActiveRecord::Migration[7.0]
def change
add_column :posts, :wrong_attributee, :integer, default: 0
end
end
class BugTest < Minitest::Test
def test_with_outdated_schema_cache
assert_equal ["id"], Post.column_names # to load schema cache
AddAttributeToPosts.migrate(:up)
AddEnumToPosts.migrate(:up)
assert_equal ["id"], Post.column_names
new_post = Post.create!(wrong_attributee: :other_value) # column is not in the schema cache, so why no error?
Post.reset_column_information
assert_equal "default_value", new_post.reload.wrong_attributee # database default is used
assert_equal "other_value", new_post.reload.wrong_attributee # actual value is 0 (sqlite default)
end
def test_unknown_attribute
AddAttributeToPosts.migrate(:up)
Post.reset_column_information
# expected/normal behaviour
assert_raises(ActiveModel::UnknownAttributeError) do
Post.create!(wrong_attribute: :other_value)
end
new_post = Post.create!(wrong_attributee: :other_value) # bug, this should raise an error
assert_nil new_post.reload.wrong_attributee
assert_nil new_post.reload.wrong_attrib
assert_equal "other_value", new_post.reload.wrong_attributee # not really expected behaviour :)
end
def teardown
Post.delete_all
AddAttributeToPosts.migrate(:down)
AddEnumToPosts.migrate(:down) if ActiveRecord::Base.connection.column_exists?(:posts, :wrong_attributee)
Post.reset_column_information
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment