我一直使用一種簡單的搜索方法來實現一個應用程序使用的幾個部分的搜索功能。方法和控制器索引操作看起來像這樣。Rails應用程序:我如何使用簡單的搜索方法搜索相關模型?
索引操作(participants_controller.rb)
def index if params[:search] @participants = Participant.search(params[:search]) else @participants = Participant.all end end
參與者模型(participant.rb)
def self.search(terms) if terms where("email ILIKE ?", "%#{terms}%") end end
遇到的問題IM是,我不僅需要通過電子郵件搜索(這是現在如何設置self.search方法。)但是我也需要通過名和姓(f_name,l_name)進行搜索。但此信息位於名爲ParticipantDetail的關聯模型中。
香港專業教育學院嘗試了以下但我得到一個「沒有方法participant_detail」錯誤
def index if params[:search] @participants = Participant.participant_detail.search(params[:search]) else @participants = Participant.all end end
香港專業教育學院還試圖尋找這樣的...
def index if params[:search] @participants = ParticipantDetail.search(params[:search]) else @participants = Participant.all end end
但其實這是沒有好,因爲它只會顯示關聯的記錄,而不會顯示歸屬的記錄(父)。
任何幫助將不勝感激。
我得到的第一選擇線 私有方法'選擇」呼籲「the_term_i以下錯誤輸入「:字符串 – Verty00
'select'不是私有方法。確保你在'Participant'上調用它,它需要是一個ActiveRecord模型(不是普通的ruby類) – Tallboy
我錯誤地實現了你的解決方案。我完全按照你的想法嘗試過,現在我得到了... 現在我得到 無法加入「參與者」到名爲'participant_details'的關聯;也許你拼錯了嗎? 我現在扮演與拼寫和多個與它似乎沒有關係。我仍然得到這個錯誤。 :/ – Verty00