Created
October 25, 2016 00:14
-
-
Save ianks/f934a6183b401254099c9e710b23217b to your computer and use it in GitHub Desktop.
ActiveRecord: unable to delete records with a `has_one, through:` relationship
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
require "bundler/inline" | |
gemfile(true) do | |
source "https://rubygems.org" | |
gem "activerecord", "5.0.0" | |
gem "sqlite3" | |
end | |
require "active_record" | |
require "minitest/autorun" | |
require "logger" | |
# Ensure backward compatibility with Minitest 4 | |
Minitest::Test = MiniTest::Unit::TestCase unless defined?(Minitest::Test) | |
# 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 :identities | |
create_table :tips | |
create_join_table :identities, :tips, table_name: :identity_tips | |
# ============================= | |
# ===== My temporary fix ====== | |
# ============================= | |
# # Adding a primary_key allieviates the issue | |
# create_table :identity_tips, id: :primary_key do |t| | |
# t.references :tip, index: true | |
# t.references :identity, index: true | |
# end | |
end | |
class Identity < ActiveRecord::Base | |
has_many :identity_tips | |
has_many :tips, through: :identity_tips | |
end | |
class IdentityTip < ActiveRecord::Base | |
belongs_to :identity | |
belongs_to :tip | |
end | |
class Tip < ActiveRecord::Base | |
has_one :identity_tip | |
has_one :identity, through: :identity_tip | |
end | |
class BugTest < Minitest::Test | |
def test_identity_is_deletable_through_tip | |
tip = Tip.create! | |
identity = Identity.create! | |
tip.update(identity: identity) | |
new_identity = Identity.create! | |
# bad things happen | |
tip.identity = new_identity | |
assert_equal(new_identity, tip.identity) | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment