Rails:has_many有自我条件

时间:2014-02-27 09:59:33

标签: ruby-on-rails associations

我正在尝试在引用当前模型的has_many子句上有一个条件,但是我收到了一个错误:

PG::Error: ERROR:  missing FROM-clause entry for table "badges"

这是我的模特:

class Badge < ActiveRecord::Base

  has_many :user_badges, :dependent => :destroy  
  has_many :users, :through => :user_badges, :conditions => ["(user_badges.era_id =? and badges.seasonal =?)", GAME_CONFIG["current_era"], true, GAME_CONFIG["current_era"], false]
end

你能指出我正确的方向吗?

谢谢!

1 个答案:

答案 0 :(得分:0)

has_many :users, -> { select(badges.*, users.*).where("user_badges.era_id =? and badges.seasonal =?", GAME_CONFIG["current_era"], GAME_CONFIG["current_era"]) }, through: :user_badges