0
find_each는 finder_sql을 사용하는 ActiveRecord 릴레이션에서 훌륭하게 재생되지 않는 것 같습니다. 전에 누구도이 문제를 다루었습니까? 예를 들면 다음과 같습니다.find_each와 finder_sql이 서로 잘 작동하지 않습니까?
class User < ActiveRecord::Base
has_many :games, :dependent => :destroy,
:finder_sql =>
proc {"SELECT * FROM games WHERE player_0_id = #{id} OR player_1_id = #{id}"}
end
class Game < ActiveRecord::Base
belongs_to :player_0, :class_name => "User", :foreign_key => "player_0_id", :inverse_of => :games
belongs_to :player_1, :class_name => "User", :foreign_key => "player_1_id", :inverse_of => :games
end
@ user.games.each를 호출하면 문제가되는 모든 게임을 반복하고 반복합니다. 그러나의 전화 @의 user.games.find_each 결과 :
ActiveRecord::StatementInvalid:
SQLite3::SQLException: no such column: games.user_id: SELECT "games".* FROM "games" WHERE "games"."user_id" = 2 AND ("games"."id" >= 0) ORDER BY "games"."id" ASC LIMIT 1000
hrmm, 재미있어 보입니다. 유일한 문제는 플레이어 중 하나가 null 일 수 있다는 것입니다 (우리가 빠른 검색을 위해 누군가를 찾으려고하는 경우). 또한 데이터베이스에 동일한 쌍의 여러 게임을 만들 수 있습니다 (이전에 다른 사람과 함께했던 게임을 재생할 수 있음). – insomniac2846
내가 당신이라면 데이터 모델을 변경할 것입니다. has_many를 사용하면 쉽게 해결할 수 있습니다. http://xyzpub.com/en/ruby-on-rails/3.2/ar-many_to_many.html을보십시오 – wintermeyer
hrrm, has_many의 through 부분에 사용할 세 번째 모델이 없습니다 – insomniac2846