问题描述:

find_each doesn't seem to be playing nice with an ActiveRecord relation that uses finder_sql. Has anyone dealt with this before? For example:

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

Calling @user.games.each works fine and iterates over all the games we care about. But calling @user.games.find_each results in:

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

网友答案:

Have a look at http://compositekeys.rubyforge.org/ to solve the given problem.

相关阅读:
Top