Sorting Out My Countries

I’ve got a database structure which has this sort of thing.

A Resort is in a particular country.

Presently the country is stored in a field in the Resort model,
Resort.country (a string).

I want to do this properly and have done the following

  1. added a new field to resort called country_id.
  2. creating a countries table Country with just name:string
  3. Load up a list of all unique countries into my Country model
  4. Resort belongs_to country
  5. Country has_many resorts
  6. Somehow I now need to add the relevant country_ids to each Resort by
    searching Resort.country and matching against Country.name

Anyone help me - is this on the right lines? and how do I do step 5 (got
the rest I think).

Anyone help me - is this on the right lines? and how do I do step 5 (got
the rest I think).

Well, you could approach it with a migration… it doesn’t have to be
ALL table creation, structure modification, etc, etc.

UP:
resorts = Resort.find(:all)
resorts.each do |resort|
if resort.country_id.nil?
country = Country.find_by_name(resort.country)
if found == store the country id in resort country_id
if not found == add the name to countries, get the new ID and stick
that in
resort country_id
end
end

DOWN:
for each resort, clear the country_id