jeudi 7 avril 2016

searching in assosiation Ruby on rails

my doctor has a profession which has a field spec that show the specialization of the doctor , profession model also has area field , while profession belongs to doctor . Please let me know how can i search doctors on basis of area and specialization. Search model has fields of name , sex , area , spec . Please provide code if you can.
this is my search model

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%>

Aucun commentaire:

Enregistrer un commentaire