在轨道上搜索Ruby

时间:2016-04-07 20:34:41

标签: ruby ruby-on-rails-3 ruby-on-rails-4

我的医生有一个专业,有专业的医生说明,专业模式也有区域,专业属于医生。请让我知道如何根据区域和专业来搜索医生。搜索模型具有名称,性别,面积,规格等字段。如果可以,请提供代码     这是我的搜索模型

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 %>
<%=@search.search_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%>

1 个答案:

答案 0 :(得分:0)

试试这个, 我认为他们不需要搜索模型,只需将此代码放入您的Doctor模型

即可
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

并在Doctor类上调用此方法,如果param[:query].present?

,则显示搜索结果