HAS_MANY通过与病情不工作 [英] has_many through with condition not working

查看:169
本文介绍了HAS_MANY通过与病情不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个模型级和模型的用户。等级与用户之间通过合作建立了许多-to-many关联。

I have a model Grade and a model User. Between grades and user is a many-to-many association through collaborations.

在user.rb

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

的作品,但我需要得到的只有等级与属性为存档= FALSE

works, but i need to get only grades with attribute "archived" = false

我tryied

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

,但它需要所有的等级,换言之,条件被忽略。

but it takes all the grades, in other words, the condition is ignored.

我可以在我的合作把这个条件,但合作是有等级和学校的关联是,和学校没有存档的领域,而这些原因的错误。

I can put in my collaborations this condition, but collaboration is in a polymorphic association with Grade and School, and a school doesn't have the archived field, and those causes an error.

任何想法?

推荐答案

这是解决办法。显然,因为合作是多态的关系,你需要指定一个SOURCE_TYPE

This was the solution. Apparently because collaboration is a polymorphic relation, you need to specify a source_type

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

这篇关于HAS_MANY通过与病情不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆