find_each和finder_sql彼此不能很好地玩耍?

时间:2013-04-02 15:42:52

标签: sql ruby-on-rails-3

find_each似乎与使用finder_sql的ActiveRecord关系不一致。以前有人处理过这个吗?例如:

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

1 个答案:

答案 0 :(得分:0)

请查看http://compositekeys.rubyforge.org/以解决给定问题。