Sto provando ad aggiornare un'applicazione di rails 3.0 alle guide 4.0. Uno dei comportamenti che ho notato è che la relazione tra i modelli ha smesso di funzionare.Rails 4 Has_many: attraverso join join con select
Supponiamo di avere i seguenti modelli:
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
ora siamo in grado di fare:
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
Questo ha funzionato per Rails 3.0, ma ora on Rails 4.0 che sto ottenendo Unknown column 'met_with_parent' in 'field list'
credo Rails 4 sta cercando di essere intelligente e non sta caricando tutte le tabelle di join date.
La vecchia sintassi funziona in Rails 4.0? – lurker
@mbratch no non funziona. Lo stesso problema si verifica. Con la vecchia sintassi Rails 4 registrerà una serie di messaggi di deprecazione. – Bill
cosa succederà se proverai select teacher_students.met_with_parent come met_with_parent? – faron