Dar clave principal compuesta en Rails
¿Cómo puedo dar una clave principal compuesta en Rails sin ninguna gema?
Mi primera tabla en el archivo de migración:
class CreateUsers < ActiveRecord::Migration
def self.up
create_table :users do |t|
t.string :userid
t.string :name
t.string :address
t.timestamps
end
end
def self.down
drop_table :users
end
end
Mi segunda tabla en el archivo de migración:
class CreateProjects < ActiveRecord::Migration
def self.up
create_table :projects do |t|
t.string :title
t.string :description
t.timestamps
end
end
def self.down
drop_table :projects
end
end
En mi archivo de esquema:
ActiveRecord::Schema.define(:version => 20110222044146) do
create_table "projects", :force => true do |t|
t.string "title"
t.string "description"
t.datetime "created_at"
t.datetime "updated_at"
end
create_table "users", :force => true do |t|
t.string "userid"
t.string "name"
t.string "address"
t.datetime "created_at"
t.datetime "updated_at"
end
end
Ahora quiero crear una tabla llamadaUser_has_project
en el que me referiré a Usuario y Proyecto, eso significa que tendrá 2 claves foráneas. Así que intenté así:
class CreateUser_has_projects < ActiveRecord::Migration
def self.up
create_table :user_has_projects do |t|
t.references :User
t.references :Project
t.boolean :status
t.timestamps
end
end
def self.down
drop_table :users
end
end
Ahora, ¿cómo puedo configurar la combinación de user_id y project_id como clave principal en user_has_projects?