Skip to content

Include association keys when importing models with explicit columns - #905

Open
OskarEichler wants to merge 1 commit into
zdennis:masterfrom
OskarEichler:codex/include-association-import-columns
Open

Include association keys when importing models with explicit columns#905
OskarEichler wants to merge 1 commit into
zdennis:masterfrom
OskarEichler:codex/include-association-import-columns

Conversation

@OskarEichler

Copy link
Copy Markdown
Contributor

Summary

When importing model instances through a collection with an explicit column list, check that selected list when adding the owner foreign key. The old code checked every schema column instead, so it omitted the foreign key from the INSERT even though the in-memory model had it.

Also include the polymorphic type column when required, matching the existing hash/value import paths.

Reproduction

For AuditParent has_many :audit_children:

parent = AuditParent.create!
parent.audit_children.import([:name], [AuditChild.new(name: 'child')], validate: false)
p AuditChild.last.audit_parent_id

Before: nil. After: the parent ID. The same failure/fix is reproduced with string column names.

For has_many :attachments, class_name: 'AuditChild', as: :attachable, importing [:name] previously leaves both attachable_id and attachable_type nil; afterward both identify the owner.

Focused checks also cover already-included string/symbol foreign keys, avoiding duplicate columns. Input column arrays remain copied, not mutated. Composite association key support and STI polymorphic naming are outside this patch.

Verification and limitations

Ruby 4.0.6 / Active Record 8.1.3.1. The unchanged existing suites pass both before and after this individual patch: PostgreSQL 15.19: 304 runs, 792 assertions; SQLite 2.2.0: 225 runs, 563 assertions; no failures/errors/skips. Targeted RuboCop, Ruby syntax and whitespace checks pass.

Checks use an isolated PostgreSQL Unix socket and an in-memory SQLite database; no production services. A temporary external verification Gemfile supplies the existing test dependencies, Minitest 5 and Ruby 4's extracted rdoc/ostruct libraries. No tests, gem versions, runtime dependencies or upstream development configuration were changed. The full historical Ruby/database matrix was not run locally.

Breaking changes

None intended. Public signatures and result shapes are unchanged; the behavior correction is described above.

Prepared with Codex assistance; the source change and reproduction were reviewed and executed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant