레거시 데이터베이스가 있습니다. 사용자에게는 많은 계정이 있고 계정에는 많은 사용자가 있습니다. UserAccountMapping이라는 조인 테이블이 있습니다.레일 3 has_and_belongs_to_many
=====================
|UserID | AccountID|
=====================
두 열 모두 var char 유형입니다. join_table => : UserAccountMapping : foreign_key => 계정 ID : association_foreign_key =>
Account.rb에서:
has_and_belongs_to_many : 사용자 아이디 User.rb에서
:
has_and_belongs_to_many : 계정 : join_table => 'UserAccountMapping', : foreign_key => : 사용자 ID는 : association_foreign_key => :
를 계정 아이디
사용자는 : email 및 : id 열도 갖습니다. 사용자의 기본 키는 다음과 같습니다. id (정수) 및 : email는 문자열입니다.
User.first.accounts
과 같은 작업을 수행하면 조인 SQL의 WHERE 절이 UserAccountMapping.UserID를 사용자의 : id와 일치 시키려고 시도하지만 사용자의 : 전자 메일을 사용하려고합니다. 이것이 가능한가?
그래서, 생성하는 SQL은 다음과 같습니다
것은 "계정"SELECT * FROM "계정"INNER는 "계정"ON "UserAccountMapping을"가입 "계정 ID"= "UserAccountMapping" "계정 ID"... .
것은 "계정"SELECT * "계정"INNER는 "계정"ON "UserAccountMapping"을 가입 FROM "계정 ID"대신 "UserAccountMapping" "아이디"= 3
, 나는 이것을 원한다.. = "UserAccountMapping". "Accoun tID "어디에서"UserAccountMapping "."UserID "="[email protected] "
왜 그렇게하고 싶습니까? 'User' 프라이 머리 키 =>를 설정해보십시오 : email – Damien