2012-08-24 2 views
1

사용자가 다른 게임을하고 각 게임 내에서 팀 요새와 같은 역할을 선택할 수있는 게임을 만들고 있습니다.3 테이블 조인 레일

나는이 테이블

create_table "games_users", :force => true do |t| 
    t.integer "game_id" 
    t.integer "user_id" 
    t.datetime "created_at", :null => false 
    t.datetime "updated_at", :null => false 
    end 

"games_users"을 가입 만들어 그리고 게임과 사용자

game.rb

has_and_belongs_to_many :users 

user.rb

간의 HABTM 관계를 구축하는 데 성공했다
has_and_belongs_to_many :games 

3 테이블 조인을 사용하여 역할 모델 (또는 역할 문자열)을 시스템에 추가하고 싶습니다. 어떻게해야합니까?

+0

당신이 조인 3 테이블이 무엇을 의미하는지에 좀 더 구체적으로 할 수 있습니까? 사용자, 역할 및 게임을 단일 레코드로 연결하는 테이블을 의미합니까? – PinnyM

+0

나는 게임 테이블에서 그들이 플레이 한 각 게임 내에서 사용자와 그들의 역할을 얻을 수 있어야하며, 사용자 테이블에서 그들이 플레이 한 게임과 각 게임 내에서의 역할을 얻는다. – henghonglee

답변

2

당신은 roles 테이블과 games_users 테이블을 교체하고 다음과 같이 UserGame 사이 has_many :through을 할 수 :

class User < ActiveRecord::Base 
    has_many :roles 
    has_many :games, :through => :roles 
end 

class Role < ActiveRecord::Base 
    belongs_to :user 
    belongs_to :game 
end 

class Game < ActiveRecord::Base 
    has_many :roles 
    has_many :users, :through => :roles 
end 
관련 문제