-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。我該怎麼做呢?

回答

0

有關範圍的has_many文檔部分顯示下面的例子:

has_many :posts, ->(blog) { where("max_post_length > ?", blog.max_post_length) } 

這個例子表明,scope可以採取一個參數,這是它被稱爲上

記錄