has_many通过条件不起作用
问题描述:
我有模型成绩和模型用户。成绩和用户之间通过合作是多对多的关联。has_many通过条件不起作用
在user.rb
has_many :grades, through: :collaborations, source: :user
的作品,但我需要得到只与属性 “归档”=假
我tryied
has_many :grades, through: :collaborations, source: :user, conditions: [' archived = ? ', false]
成绩,但它需要所有的等级换句话说,条件被忽略。
我可以把这个条件放在我的合作中,但是合作是与学校和学校有多形联系的,一所学校没有存档的领域,而这些都会导致错误。
任何想法?
答
尝试使用此
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]
他们不工作:( – vladCovaliov 2013-04-27 08:07:04