All threads / How to import data with many associations?

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?

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

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

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

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

    logo Created with Sketch.

    Ruby on Rails tutorials, guides, and screencasts for web developers learning Ruby, Rails, Javascript, Turbolinks, Stimulus.js, Vue.js, and more. Icons by Icons8

    © 2020 GoRails, LLC. All rights reserved.