What is the difference between using t.integer and t.reference to define foreign keys in rail migration

Given two classes, Apple and Orange:

def class Apple < ActiveRecord::Base
  has_and_belongs_to_many :oranges
end

def class Orange < ActiveRecord::Base
  has_and_belongs_to_many :apples
end

What is the difference between using t.integer to define foreign keys in a join table:

create_table :apples_oranges, :id => false do |t|
  t.integer :apple_id
  t.integer :orange_id
end

and using t.references to define foreign keys in the connection table:

create_table :apples_oranges, :id => false do |t|
  t.references :apple
  t.references :orange
end

I have seen both, and they seem to be interchangeable. I just wanted to make sure that there wasnโ€™t any subtlety / magic that I was missing.

Oh and I'm on Rails 3.2 w / MySQL

+5
source share
1 answer

All Articles