Skip to content

Commit

Permalink
Fix destroy_all for has_many :through associations that points to…
Browse files Browse the repository at this point in the history
… a CPK model
  • Loading branch information
nvasilevski committed Jun 14, 2023
1 parent 01363bd commit 892e4ae
Show file tree
Hide file tree
Showing 8 changed files with 72 additions and 4 deletions.
Expand Up @@ -62,9 +62,8 @@ def construct_join_attributes(*records)
if Array(association_primary_key) == reflection.klass.composite_query_constraints_list && !options[:source_type]
join_attributes = { source_reflection.name => records }
else
join_attributes = {
source_reflection.foreign_key => records.map(&association_primary_key.to_sym)
}
assoc_pk_values = records.map { |record| record._read_attribute(association_primary_key) }
join_attributes = { source_reflection.foreign_key => assoc_pk_values }
end

if options[:source_type]
Expand Down
Expand Up @@ -37,13 +37,15 @@
require "models/seminar"
require "models/session"
require "models/sharded"
require "models/cpk"

class HasManyThroughAssociationsTest < ActiveRecord::TestCase
fixtures :posts, :readers, :people, :comments, :authors, :categories, :taggings, :tags,
:owners, :pets, :toys, :jobs, :references, :companies, :members, :author_addresses,
:subscribers, :books, :subscriptions, :developers, :categorizations, :essays,
:categories_posts, :clubs, :memberships, :organizations, :author_favorites,
:sharded_blog_posts, :sharded_tags, :sharded_blog_posts_tags
:sharded_blog_posts, :sharded_tags, :sharded_blog_posts_tags, :cpk_orders, :cpk_tags,
:cpk_order_tags

# Dummies to force column loads so query counts are clean.
def setup
Expand Down Expand Up @@ -440,6 +442,16 @@ def test_destroy_all
assert_empty posts(:welcome).people.reload
end

def test_destroy_all_on_composite_primary_key_model
tag = cpk_tags(:cpk_tag_loyal_customer)

assert_not_empty(tag.orders.to_a)

tag.orders.destroy_all
assert_empty(tag.orders)
assert_empty(tag.orders.reload)
end

def test_destroy_all_on_association_clears_scope
post = Post.create!(title: "Rails 6", body: "")
people = post.people
Expand Down
15 changes: 15 additions & 0 deletions activerecord/test/fixtures/cpk_order_tags.yml
@@ -0,0 +1,15 @@
_fixture:
model_class: Cpk::OrderTag

cpk_first_order_loyal_customer:
tag_id: <%= ActiveRecord::FixtureSet.identify(:cpk_tag_loyal_customer) %>
order_id: <%= ActiveRecord::FixtureSet.composite_identify(:cpk_groceries_order_1, [:shop_id, :id])[:id] %>

cpk_second_order_loyal_customer:
tag_id: <%= ActiveRecord::FixtureSet.identify(:cpk_tag_loyal_customer) %>
order_id: <%= ActiveRecord::FixtureSet.composite_identify(:cpk_groceries_order_2, [:shop_id, :id])[:id] %>

cpk_first_order_digital_product:
tag_id: <%= ActiveRecord::FixtureSet.identify(:cpk_tag_digital_product) %>
order_id: <%= ActiveRecord::FixtureSet.composite_identify(:cpk_groceries_order_1, [:shop_id, :id])[:id] %>

11 changes: 11 additions & 0 deletions activerecord/test/fixtures/cpk_tags.yml
@@ -0,0 +1,11 @@
_fixture:
model_class: Cpk::Tag

cpk_tag_loyal_customer:
name: "Loyal customer"

cpk_tag_digital_product:
name: "Digital product"

cpk_tag_ruby_on_rails:
name: "Ruby on Rails"
2 changes: 2 additions & 0 deletions activerecord/test/models/cpk.rb
Expand Up @@ -5,3 +5,5 @@
require_relative "cpk/order"
require_relative "cpk/review"
require_relative "cpk/order_agreement"
require_relative "cpk/order_tag"
require_relative "cpk/tag"
10 changes: 10 additions & 0 deletions activerecord/test/models/cpk/order_tag.rb
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Cpk
class OrderTag < ActiveRecord::Base
self.table_name = :cpk_order_tags

belongs_to :tag
belongs_to :order, primary_key: :id
end
end
10 changes: 10 additions & 0 deletions activerecord/test/models/cpk/tag.rb
@@ -0,0 +1,10 @@
# frozen_string_literal: true

module Cpk
class Tag < ActiveRecord::Base
self.table_name = :cpk_tags

has_many :order_tags
has_many :orders, through: :order_tags
end
end
9 changes: 9 additions & 0 deletions activerecord/test/schema/schema.rb
Expand Up @@ -267,6 +267,15 @@
t.string :status
end

create_table :cpk_order_tags, force: true do |t|
t.integer :order_id
t.integer :tag_id
end

create_table :cpk_tags, force: true do |t|
t.string :name, null: false
end

create_table :cpk_order_agreements, force: true do |t|
t.integer :order_id
t.string :signature
Expand Down

0 comments on commit 892e4ae

Please sign in to comment.