我有一个应用程序,我们只是从2.1.0升级到2.3.10。升级后,之前工作的关联扩展会导致失败。以下是扩展模型的代码:
Class Classroom
has_many :registrations
has_many :students, :through => :registrations, :uniq => true do
def in_group(a_group)
if a_driver
scoped(:conditions => ['registrations.group_id = ?', a_group])
else
in_no_group
end
end
def in_no_group
scoped(:conditions => 'registrations.group_id is null')
end
end
end
这是我实际问题的简化模型,但基本上我曾经能够做到
classroom.students.in_group(honor_students)
这不再有效,输出如下:
classroom.students.in_group(honor_students)
ActiveRecord::StatementInvalid: Mysql::Error: Unknown column 'registrations.group_id' in 'where clause': SELECT * FROM `students` WHERE (registrations.group_id = 1234)
当我刚刚获取学生列表时,SQL具有以上版本中缺少的所有预期的连接语法:
SELECT DISTINCT `students`.* FROM `students` INNER JOIN `registrations` ON `students`.id = `registrations`.student_id WHERE ((`registrations`.classroom_id = 9876))
为什么关联扩展缺少所有连接SQL?
答案 0 :(得分:1)
在学生身上使用此Rails 2.3: How to turn this SQL statement into a named_scope,而不是将其嵌套在关联中