has_many通过条件不起作用

我有一个模型等级和一个模型用户。 在成绩和用户之间通过协作实现多对多关联。

在user.rb中

has_many :grades, through: :collaborations, source: :user 

工作,但我只需要获得属性“archived”= false的成绩

我试过了

 has_many :grades, through: :collaborations, source: :user, conditions: [' archived = ? ', false] 

但它需要所有等级,换句话说,条件被忽略。

我可以在这种情况下加入我的合作,但协作与Grade和School有多态关系,而且学校没有存档字段,这些都会导致错误。

有任何想法吗?

试试这个

 has_many :grades, through: :collaborations, source: :user, :conditions => { archived: false} 

要么

 has_many :grades, through: :collaborations, source: :user, :conditions => { 'grades.archived' => false } 

这是解决方案。 显然,因为协作是一种多态关系,您需要指定source_type

  has_many :grades, through: :collaborations, source: :owner, source_type: "Grade", conditions: ['archived = ? ', false]