我有一个模型等级和一个模型用户。等级和用户之间是通过协作实现的多对多关联。
在user.rb中
has_many :grades, through: :collaborations, source: :user工作,但我只需要获得属性为"archived“= false的分数
我试过了
has_many :grades, through: :collaborations, source: :user, conditions: [' archived = ? ', false]但是它需要所有的分数,换句话说,条件被忽略了。
我可以在我的协作中加入这个条件,但是协作与年级和学校存在多态关联,并且学校没有存档字段,这些都会导致错误。
有什么想法吗?
发布于 2013-04-27 05:17:48
试着使用这个
has_many :grades, through: :collaborations, source: :user, :conditions => { archived: false}或
has_many :grades, through: :collaborations, source: :user, :conditions => { 'grades.archived' => false }发布于 2013-04-27 16:26:14
这就是解决方案。显然,因为协作是一种多态关系,所以需要指定一个source_type
has_many :grades, through: :collaborations, source: :owner, source_type: "Grade", conditions: ['archived = ? ', false]https://stackoverflow.com/questions/16241015
复制相似问题