sarteleb / tc359_rails_final

0 stars 0 forks source link

One to many relationship #1

Open sarteleb opened 9 years ago

sarteleb commented 9 years ago

@chrisvfritz Citizen should be able to have as many animal licenses as they want. Let me know if i need to fix anything :).

chrisvfritz commented 9 years ago

Migrations

I'm getting an error trying to migrate the database.

rake aborted!
ActiveRecord::DuplicateMigrationNameError:
Multiple migrations have the name CreateAnimalLicenses

It looks like when you added the animal_type and citizen_id, you might have used rails generate model when that model already exists. Instead, you probably just want to use rails generate migration. That's the command used to modify an existing model/table.

The migration command you probably wanted is:

rails generate migration AddAnimalTypeAndCitizenIdToAnimalLicenses animal_type:text citizen:references

Which generates:

db/migrate/20150416171254_add_animal_type_and_citizen_id_to_animal_licenses.rb
class AddAnimalTypeAndCitizenIdToAnimalLicenses < ActiveRecord::Migration
  def change
    add_column :animal_licenses, :animal_type, :text
    add_reference :animal_licenses, :citizen, index: true
  end
end

The add_reference command above does add a citizen_id column of type integer. That column is also helpfully indexed by default, so that you can grab all the animal licenses for a specific citizen quickly, even in a large database.

While we're in db/migrate, I see a couple empty migrations called DropTable and DropCars. Since they're not doing anything, they can simply be deleted.

Models

I'm not seeing a model for the citizens table or a citizen-to-animal_licenses relationship used anywhere in your app. The resources on the skill page should help in figuring out how to define those relationships in your models.