spike
spike

Reputation: 10004

Using seed-fu to seed data with associations (from csv)

I have a large csv file (must be spreadsheet-editable) of countries and regions. Countries have_many regions.

The file has multiple lines for the same country, i.e:

USA, New York
USA, Pennsylvania

etc.

I'd like to use seed-fu to seed my database with this information. I think I should use Writer to take the CSV file and generate a seed file, but I'm not sure how to do this while retaining the associations. Is there a standard method for this?

Upvotes: 1

Views: 1120

Answers (1)

spike
spike

Reputation: 10004

I ended up doing this without Writer. My answer is:

  • Process the file line by line, creating countries and regions as you go
  • For each line, first try to create the country. Then create the region using region_name and the country_id as the constraints to seed_fu's seed method.
  • I avoid the inefficiency of calling seed multiple times on each country (which would be fine, but slow) by writing a wrapper to seed and using memoization.

Upvotes: 1

Related Questions