Skip to content

Commit

Permalink
Merge pull request zdennis#783 from Booster-Apps/composite-primary-ke…
Browse files Browse the repository at this point in the history
…ys-required-belongs-to

Fix importing models that are required to belong to models with composite primary keys
  • Loading branch information
jkowens authored Sep 28, 2022
2 parents 20c1d3a + 15ed95c commit e322b7b
Show file tree
Hide file tree
Showing 6 changed files with 33 additions and 1 deletion.
1 change: 1 addition & 0 deletions gemfiles/7.0.gemfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# frozen_string_literal: true

gem 'activerecord', '~> 7.0.0'
gem 'composite_primary_keys', '~> 14.0'
2 changes: 1 addition & 1 deletion lib/activerecord-import/import.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ def init_validations(klass)
end
end

filter.instance_variable_set(:@attributes, attrs)
filter.instance_variable_set(:@attributes, attrs.flatten)

if @validate_callbacks.respond_to?(:chain, true)
@validate_callbacks.send(:chain).tap do |chain|
Expand Down
19 changes: 19 additions & 0 deletions test/import_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,25 @@
Tag.import columns, values, validate: false
end
end

it "should import models that are required to belong to models with composite primary keys" do
tag = Tag.create!(tag_id: 1, publisher_id: 1, tag: 'Mystery')
valid_tag_alias = TagAlias.new(tag_id: tag.tag_id, parent_id: tag.publisher_id, alias: 'Detective')
invalid_tag_aliases = [
TagAlias.new(tag_id: nil, parent_id: nil, alias: 'Detective'),
TagAlias.new(tag_id: tag.tag_id, parent_id: nil, alias: 'Detective'),
TagAlias.new(tag_id: nil, parent_id: tag.publisher_id, alias: 'Detective'),
]

assert_difference "TagAlias.count", +1 do
TagAlias.import [valid_tag_alias]
end
invalid_tag_aliases.each do |invalid_tag_alias|
assert_no_difference "TagAlias.count" do
TagAlias.import [invalid_tag_alias]
end
end
end
end
end

Expand Down
1 change: 1 addition & 0 deletions test/models/tag.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,5 @@
class Tag < ActiveRecord::Base
self.primary_keys = :tag_id, :publisher_id unless ENV["SKIP_COMPOSITE_PK"]
has_many :books, inverse_of: :tag
has_many :tag_aliases, inverse_of: :tag
end
5 changes: 5 additions & 0 deletions test/models/tag_alias.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
# frozen_string_literal: true

class TagAlias < ActiveRecord::Base
belongs_to :tag, foreign_key: [:tag_id, :parent_id], required: true
end
6 changes: 6 additions & 0 deletions test/schema/generic_schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,12 @@
PRIMARY KEY (tag_id, publisher_id)
);
).split.join(' ').strip

create_table :tag_aliases, force: :cascade do |t|
t.integer :tag_id, null: false
t.integer :parent_id, null: false
t.string :alias, null: false
end
end

create_table :customers, force: :cascade do |t|
Expand Down

0 comments on commit e322b7b

Please sign in to comment.