Ask A Question

Notifications

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?

Reply

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.

Reply

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

Reply

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

Reply
Join the discussion

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

Join 73,895+ 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. Icons by Icons8

    © 2023 GoRails, LLC. All rights reserved.