Rails 3.0 アプリを Rails 4.0 にアップグレードしようとしています。私が気付いた動作の 1 つは、モデル間の関係が機能しなくなったことです。
次のモデルがあるとします。
class Student < ActiveRecord::Base
has_many :teacher_students
has_many :teachers, :through => :teacher_students, :select => 'teacher_students.met_with_parent, teachers.*'
# The Rails 4 syntax
has_many :teachers, -> { select('teacher_students.met_with_parent, teachers.*') }, :through => :teacher_students
end
class Teacher < ActiveRecord::Base
has_many :teacher_students
has_many :students, :through => :teacher_students, :select => 'teacher_students.met_with_parent, students.*'
end
class TeacherStudent < ActiveRecord::Base
belongs_to :teacher
belongs_to :student
# Boolean column called 'met_with_parent'
end
これで、次のことができるようになりました。
teacher = Teacher.first
students = teacher.students
students.each do |student|
student.met_with_parent # Accessing this column which is part of the join table
end
これはRails 3.0では機能しましたが、Rails 4.0では、Unknown column 'met_with_parent' in 'field list'
Rails 4が賢くしようとしていて、指定された結合テーブル全体をロードしていないと信じています.