Rails has_many通过多态source_type scope_chain bug

时间:2013-07-10 21:10:06

标签: ruby-on-rails polymorphic-associations

好吧,自从我设置了一些多态的时候,一些奇怪的东西已经发生了:通过关系。

我使用Rails 3.2.12和Ruby 1.9.3。

关系如下:

class User < ActiveRecord::Base

has_many :registrations

has_many :student_learning_component_statuses, :through => :registrations

has_many :programs, :through => :student_learning_component_statuses, :source => :statusable, :source_type => 'Program'
has_many :phases, :through => :student_learning_component_statuses, :source => :statusable, :source_type => 'Phase'

现在,当我查询用户的程序或阶段时,无论我查询第一次总是成功,查询的关系第二次都会返回一个空数组。< / p>

控制台使用结果如下所示:

1.9.3p392 :005 > User.find_by_email('s1@nyfs.com').programs
  User Load (1.4ms)  SELECT "users".* FROM "users" WHERE "users"."email" = 's1@nyfs.com' LIMIT 1
  Program Load (1.4ms)  SELECT "programs".* FROM "programs" INNER JOIN "student_learning_component_statuses" ON "programs"."id" = "student_learning_component_statuses"."statusable_id" INNER JOIN "registrations" ON "student_learning_component_statuses"."registration_id" = "registrations"."id" WHERE "registrations"."user_id" = 137 AND ("student_learning_component_statuses"."statusable_type" = 'Program')
 => [#<Program id: 1, title: "Beginner Certification", description: "", school_id: 47, created_at: "2013-06-27 19:46:44", updated_at: "2013-06-27 22:13:27", duration: "16 weeks">, #<Program id: 2, title: "Instructor Certification", description: "Another Program to test more stuff", school_id: 47, created_at: "2013-06-28 02:14:43", updated_at: "2013-06-28 02:14:43", duration: "20 weeks">] 
1.9.3p392 :006 > User.find_by_email('s1@nyfs.com').phases
  User Load (0.9ms)  SELECT "users".* FROM "users" WHERE "users"."email" = 's1@nyfs.com' LIMIT 1
  Phase Load (0.7ms)  SELECT "phases".* FROM "phases" INNER JOIN "student_learning_component_statuses" ON "phases"."id" = "student_learning_component_statuses"."statusable_id" INNER JOIN "registrations" ON "student_learning_component_statuses"."registration_id" = "registrations"."id" WHERE "registrations"."user_id" = 137 AND ("student_learning_component_statuses"."statusable_type" = 'Program' AND "student_learning_component_statuses"."statusable_type" = 'Phase')
 => [] 

注意第二个查询的结尾检查statusable_type = 'Program' AND statusable_type = 'Phase',这将永远不会是这种情况。有没有人遇到这样的问题?是时候升级到Rails 4了吗?或者我的人际关系可能出现问题。

如果有更多代码可能有用,请告诉我,并提前感谢任何建议。

[更新]

根据我的研究,这个问题的修复程序仍未集成到Rails 4主分支中,因此我将继续将升级推迟到Rails 4。

1 个答案:

答案 0 :(得分:2)

显然,这个问题虽然很少见,但却为rails团队所知:

问题描述如下: https://github.com/rails/rails/issues/3882

基本问题: https://github.com/rails/rails/pull/10538

可能提供修复的代码补丁: https://github.com/rails/rails/commit/b644c51c0ad22ff309d9717b7e9a3bfbc856a8c4

我没有修补代码,而是选择按照第一个链接中列出的“黑客”进行操作。对于我的特定用例,hack如下(我已经验证它确实有效):

  has_many :_users_programs, :through => :registrations, :source => :student_learning_component_statuses # workaround (do not use directly)
  has_many :programs, :source_type => 'Program', :source => :statusable, :through => :_users_programs

  has_many :_users_phases, :through => :registrations, :source => :student_learning_component_statuses # workaround (do not use directly)
  has_many :phases, :source_type => 'Phase', :source => :statusable, :through => :_users_phases
相关问题