2016-04-07 8 views
0

私の医者は職業が医者に属している間、医師の専門化を示すフィールドスペックを持つ職業を持っており、職業モデルも分野を持っています。地域や専門分野に基づいて医者を検索するにはどうすればいいのか教えてください。検索モデルには名前、性別、面積、スペックのフィールドがあります。可能であれば、コードを提供してください。
この私の検索モデルアソシエーションで検索するRuby on rails

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?

の場合、検索結果を表示している場所