Skip to content

Importing an array of hashes ignores columns not in the first hash #507

Closed
@ahmad-sanad

Description

Title says it all. Say we have table Foo with columns id, bar, baz, and want to import the following:

arr = [
  { bar: 'abc' },
  { baz: 'xyz' },
  { bar: '123', baz: '456' }
]

Foo.import arr

This creates the rows:

+----+------+------+
| id | bar  | baz  |
+----+------+------+
| 1  | abc  | null |
+----+------+------+
| 2  | null | null |
+----+------+------+
| 3  | 123  | null |
+----+------+------+

instead of the expected:

+----+------+------+
| id | bar  | baz  |
+----+------+------+
| 1  | abc  | null |
+----+------+------+
| 2  | null | xyz  |
+----+------+------+
| 3  | 123  | 456  |
+----+------+------+

It's possible to get around this by using ActiveRecord objects, for example:

arr.map! { |args| Foo.new(args) }
Foo.import arr

but this is still a rather pernicious bug.

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions