Topic: How to ensure id exists

class CreateSpaces < ActiveRecord::Migration
  def change
    create_table :spaces do |t|
      t.string :Type
      t.integer :ship_id


How do I make it so when I create a new space, the user must enter a valid ship_id (i.e. the user cannot enter '2' for the ship_id if there is no ship with that id)?

Any help is appreciated.

Re: How to ensure id exists

See Rails guide validations; … ion-happen which is the preferable approach.
You could also use 'add_index' in your migration:

Last edited by Javix (2012-04-02 09:19:07)