Understanding Library: ActiveRecord Import
It’s important to use external programming libraries, as it improves your output. At the same time, not fully understanding the library can get you in trouble. This is exactly what happened to me recently. I realize I need to understand a library.
I share an example of how I went through to understanding
on_duplicate_update function. Sharing the tests I used to validate
This article will take you less than three minutes to read.
activerecord-import, I had questions on its behavior. We
were using it as:
When asked what exactly was it doing, I realize I didn’t understand
how it worked. I didn’t have a razor sharp understanding of:
conflict_target and the columns listed.
I read the documentation but it wasn’t clear to me. So, I made tests to prove out what’s going on.
The situation came up when inserting the same item with different values. What would happen?
That works, now, what if we import another item with a new value for
Oh, this didn’t update
column_3 to the new value. Why?
Read the Manual!
for ActiveRecord Import lists out options for
on_duplicate_keys_update such as:
One way to update
column_3 to a new value would be:
This will update
column_3 values to the value in
Just update it
Is there a way to update all columns all the time? In this example, there’s only one, when there are bazillion columns, specifying each one gets annoying.
Yes, there is! The documentation is not explicit in updating all
The syntax to update all column values on import is:
This will work, regardless of uniqueness constraints.
conflict_target used in this case? That seems to be the
source of confusion in the first place.
It’s to satisfy any uniqueness constraint setup in the database and specify the columns to use as key identifiers.
In this case:
column attribute is not present, no updates would happen,
even if there is a conflicting entry. Effectively having a
configuration of updating no columns.
column_2 as the key identifiers and have
updates done on
column_3, the syntax would be:
This is an option to control updates to columns and conflict resolution.
columns: :all would have the same behavior here too.)
Using external libraries are an effective manner to increase one’s output when programming. Effectively, another team of programmers are working on solving that problem.
When one doesn’t fully understand how external library works, they can get into trouble and make wrong decisions to cause double (or more) work later on.