-1
我有以下設置:試圖引用自一個的has_many範圍內的軌道5
class TeamEnrollment < ApplicationRecord
belongs_to :team
has_many :term_enrollments, through: :team
end
上面的代碼工作正常。現在,我需要改變的has_many線看起來像這樣:
has_many :term_enrollments,
-> {where("(term_enrollments.term_start_date <= ? AND term_enrollments.term_end_date >= ?) OR (term_enrollments.term_start_date >= ? AND term_enrollments.term_end_date > ?) OR (term_enrollments.term_start_date <= ? AND term_enrollments.term_end_date >= ?)", self.start_date, self.start_date, self.start_date, self.termination_date, self.termination_date, self.termination_date)}
, through: :team
從上面的代碼中,我得到,如果我嘗試下面的錯誤做TeamEnrollment.first.term_enrollments
:
undefined method `start_date' for #<TermEnrollment::ActiveRecord_Relation:0x007ff706550550>
我已經測試where
條款與上面的代碼的第一個配置,它工作正常。只有在has_many
條款中它不起作用。 我很確定它與使用has_many through
內部的自我有關。我正在嘗試引用TeamEnrollment的start_date。我該怎麼做呢?