0

This post很好地描述了我的问题,但该帖子的答案假设您为子类使用单表继承(STI)。在我的情况下,SomeProf的子类有它们自己的表,并且由于_type列存储超类的类名,所以这不起作用。在我相当于SomeDeepProf1我宣布self.table_name = 'some_other_table',我不喜欢使用STI,因为我的等效SomeProf的子类具有非常不同的属性。Rails没有STI的多态关联

任何建议如何解决这个问题?

回答

0

现在我用以下解决方法在SomeDeepProf1类解决这个问题: before_create :set_poly_type_to_class_name ... def set_poly_type_to_class_name self.profile_type = self.profile.class.name end

+0

不幸的是,这是不行的,一路...看来,因为它是类'SomeDeepProf1'是想'profile_type '应该是'SomeProf'而不是'SomeDeepProf1'。在我的模型中,我有'SomeProf'类的has_many:through关系,如果我试图从'SomeDeepProf1'到has_many:through关系,查询将是'[“profile_type”,“SomeProf”]'。我怎样才能改变查询来检查'SomeDeepProf1'而不是'SomeProf'? – ehannes