문제

나는 dwilkie를 사용하고 있습니다 외국인 레일 용 플러그인. 나는 다음과 같은 테이블 제작 진술이 있습니다.

create_table "agents_games", :force => true, :id => false do |t|
  t.references :agents,     :column => :agent_id, :foreign_key => true, :null => false
  t.references :games,      :column => :game_id, :foreign_key => true, :null => false
end

그러나 이것은 다음 SQL을 생성합니다.

[4;35;1mSQL (2.7ms)[0m [0mCREATE TABLE "agents_games" ("agents_id" integer NOT NULL, "games_id" integer NOT NULL) [0m

열이 호출되기를 원합니다 agent_id 그리고 game_id - 아니다 agents_id 그리고 games_id. 레일이 기둥을 복수화하는 것을 방지하려면 어떻게해야합니까?


나는 내에서 다음을 시도했다 enviornment.rb 도움이되지 않은 파일 :

ActiveSupport::Inflector.inflections do |inflect|
  inflect.uncountable "agent_id", "game_id"
end
도움이 되었습니까?

해결책 2

내 문제에 대한 해결책을 찾았습니다. 나는 다음과 같은 참고 문헌과 별도로 외국 키를 선언해야했습니다.

create_table "agents_games", :force => true, :id => false do |t|
  t.references :agent
  t.foreign_key :agents,     :column => :agent_id, :null => false
  t.references :game
  t.foreign_key :games,      :column => :game_id, :null => false
end

이것으로, 나는 인기를 꺼낼 수있었습니다.

다른 팁

일반적으로 Activerecord의 컨벤션과 싸우지 마십시오. AR이 잘 작동하는 것의 일부입니다. 그러나 이것을 위해 하나의 사례 당신은 예외를 만들고 싶습니다. 환경의 일부 코드를 통해 충분히 쉽습니다 .RB, 확인하십시오. http://api.rubyonrails.org/classes/inflector/inflections.html 예를 들어.

참조에서 단수형 모델 이름을 사용하면 다음과 같이 원하는 것을 얻을 수 있다고 생각합니다.

create_table "agents_games", :force => true, :id => false do |t|
  t.references :agent,     :foreign_key => true, :null => false
  t.references :game,      :foreign_key => true, :null => false
end

이는 조인 테이블의 각 행에 하나의 에이전트 _id와 하나의 game_id가 있다는 것을 반영하여 명확한 방법입니다. 따라서 하나의 에이전트가 하나의 게임을 참조합니다.

이 경우 추가 변곡 또는 외국의 주요 선언은 필요하지 않습니다.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top