Skip to content

Instantly share code, notes, and snippets.

@vpivo
Created May 20, 2013 22:19
Show Gist options
  • Save vpivo/5616054 to your computer and use it in GitHub Desktop.
Save vpivo/5616054 to your computer and use it in GitHub Desktop.
Renaming an association
class Game < ActiveRecord::Base
has_and_belongs_to_many :users
belongs_to :winner, :class_name => "User"
end
class User < ActiveRecord::Base
has_and_belongs_to_many :games
validates :name, :uniqueness => true
end
class GamesUser < ActiveRecord::Base
end
class CreateUsers < ActiveRecord::Migration
def change
create_table :users do |t|
t.string :name
end
end
end
class CreateGames < ActiveRecord::Migration
def change
create_table :games do |t|
t.integer :winner_id
t.timestamps
end
end
end
class CreateGamesUsers < ActiveRecord::Migration
def change
create_table :games_users do |t|
t.integer :user_id
t.integer :game_id
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment