2016-04-07 103 views
0

我的醫生有一個專業有專業的醫生顯示專業,職業模式也有專業領域,專業屬於醫生。請讓我知道如何根據地區和專業搜索醫生。搜索模型包含名稱,性別,區域,規格等字段。如果可以,請提供代碼。
這是我的搜索模式搜索相關搜索紅寶石軌道上

class Search < ActiveRecord::Base 
#to help in searching methodolgy 
    belongs_to :patient 
    def search_docs 
    doctors = Doctor.all; 
    profession = Profession.all; 
    doctors = doctors.where(["name LIKE ?",name]) if name.present? 
    doctors = doctors.where(["sex LIKE ?",sex]) if sex.present? 
    doctors = profession.where(["spec LIKE ?",spec]) if spec.present? 
    doctors = doctors.where(["area LIKE ?",doctors.profession.area]) if area.present? 
    return doctors 
    end 
end 

while in my controller 

def show 
    @search = Search.find(params[:id]) 

end 

and in my show view 

<% if @search.search_docs %> 
<%[email protected]_docs.inspect%> 
    <% @search.search_docs.each do |c| %> 
     <div class="search-results"> 
     <div class="row"><div class="col-md-1"> <img class="doc-profile-pic img img-rounded" alt="pciture"></div> 
     <div class="col-md-9 search-heading"><%=c.name%> 
      <div class="row"><div class="search-details"><% if c.profession%><%=c.profession.spec%><%else%><%="Nothing"%><%end%></div></div> 
      <div class="row"><div class="search-details1"><% if c.profession%><%=c.profession.fee%><%else%><%="Nothing"%><%end%></div></div> 
      <%=link_to "Profile", :controller => "doctors", :action => "profile", :id =>c.id , :patid => @search.patient_id %> 
     </div> 
     </div> 
     </div> 
    <%end%> 

<% else %> 
<p>Sorry, we have no physician that match your criteria. </p> 
<p>We are adding new Doctors daily, come back soon</p> 

<%end%> 

回答

0

試試這個, 我覺得自己沒必要搜索模式,簡單地把這個代碼在你的醫生的模型

def self.search(query) 
    final_query = [] 
    final_query.push("professions.spec = '#{query[:spec]}'") if query[:spec].present? 
    final_query.push("professions.area = '#{query[:area]}'") if query[:area].present?  
    doctors.joins(:profession).where(final_query.join(" AND ")) 
    end 

,並呼籲醫生級別這個方法你在哪裏顯示搜索結果如果param[:query].present?