我有一个这样的模型:
Stem
-id
-etc
然后我有
Stemrelation
-stem_id
-related_stem_id
-active
我可以通过以下关系获得相关的词干
class Stem < ActiveRecord::Base
has_many :stemrelations
has_many :related_stems, :through => :stemrelations
end
class Stemrelation < ActiveRecord::Base
belongs_to :stem
belongs_to :related_stem, :class_name => "Stem", :foreign_key => "related_stem_id"
end
但现在我只想得到积极的关系。
我尝试将其添加到Stem模型中:
has_many :active_related, :through => :stemrelations, :source => :related_stem, :conditions => {:active => true}
但这给了我一个错误,因为它试图检查干模型上的活动标志而不是干相关。我在这里有什么变化?
谢谢!
答案 0 :(得分:1)
您确定需要条件关联吗?可能是named_scope适合这里:
class Stem < ActiveRecord::Base
has_many :stemrelations
has_many :related_stems, :through => :stemrelations
named_scope :active, :conditions => {:active => true}
end
你可以像这样使用它:
Stem.first.related_stems.active
答案 1 :(得分:0)
对于条件,您必须使用SQL语法。