Ask A Question


You’re not receiving notifications from this thread.

How to import data with many associations?

Daniel Weaver asked in General

I have an upcoming situation where I'll be importing a lot of data with many associations into a Rails app. The data will show the links between the objects using the original native database IDs.

When I import the data (not sure how I'll do it yet, probably CSV) what would be a good way to set up the correct associations?

I'm thinking of writing the original IDs of each object to an old_id column and using that somehow to create the associations in the database.

I'm concerned about the depth of associations here. Less than a dozen objects but all heavily associated to each other.

Any thoughts? Have I explained this well enough?


You can specify the column on your association that's used for joining tables. You'd just specify that old_id column and you should be good to go.


Chris - how will this work with the associations already set up in the app?


Create new ones like has_many :old_associations, foreign_key: :old_id or whatever options are necessary.

Join the discussion
Create an account Log in

Want to stay up-to-date with Ruby on Rails?

Join 79,047+ developers who get early access to new tutorials, screencasts, articles, and more.

    We care about the protection of your data. Read our Privacy Policy.

    Screencast tutorials to help you learn Ruby on Rails, Javascript, Hotwire, Turbo, Stimulus.js, PostgreSQL, MySQL, Ubuntu, and more.

    © 2023 GoRails, LLC. All rights reserved.