Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Removing association with dependent: :destroy, through nested attributes leaves entity from intermediate table in memory #37649

Closed
pn-y opened this issue Nov 6, 2019 · 1 comment
Labels

Comments

@pn-y
Copy link

pn-y commented Nov 6, 2019

Steps to reproduce

# frozen_string_literal: true

require 'bundler/inline'

gemfile(true) do
  source 'https://rubygems.org'

  git_source(:github) { |repo| "https://github.com/#{repo}.git" }

  gem "activerecord", '~> 6'
  gem 'sqlite3'
end

require 'active_record'
require 'minitest/autorun'
require 'logger'

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|
  end

  create_table :user_pings, force: true do |t|
    t.integer :user_id
    t.integer :ping_id
  end

  create_table :pings, force: true do |t|
  end
end

class User < ActiveRecord::Base
  has_many :user_pings, inverse_of: :user
  has_many :pings, through: :user_pings, inverse_of: :users

  accepts_nested_attributes_for :user_pings, allow_destroy: true
end

class UserPing < ActiveRecord::Base
  belongs_to :user, inverse_of: :user_pings
  belongs_to :ping, inverse_of: :user_pings
  accepts_nested_attributes_for :ping, allow_destroy: true
end

class Ping < ActiveRecord::Base
  has_many :user_pings, dependent: :destroy, inverse_of: :ping
  has_many :users, through: :user_pings, inverse_of: :pings
end

class AssociationTest < Minitest::Test
  def setup
    user = User.create
    user.pings.create
  end

  def test_association
    user = User.first
    user_ping = UserPing.first
    ping = Ping.first

    user.update(user_pings_attributes: [{ id: user_ping.id, ping_attributes: { id: ping.id, _destroy: true } } ])

    assert(user.pings.length == 0)
    assert(user.user_pings.length == 0)
  end
end

Last assert will fail.

Expected behavior

Both associations should be blank without user.reload

Actual behavior

Association from through table still available on user object

System configuration

Rails version: 5.2.3

Ruby version: ruby 2.5.1p57

@rails-bot
Copy link

rails-bot bot commented Feb 4, 2020

This issue has been automatically marked as stale because it has not been commented on for at least three months.
The resources of the Rails team are limited, and so we are asking for your help.
If you can still reproduce this error on the 6-0-stable branch or on master, please reply with all of the information you have about it in order to keep the issue open.
Thank you for all your contributions.

@rails-bot rails-bot bot added the stale label Feb 4, 2020
@rails-bot rails-bot bot closed this as completed Feb 11, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

1 participant