Rails搜索多对多

时间:2016-01-04 14:24:15

标签: ruby-on-rails-4

我已根据多对多关系创建了一个Rails应用。有三个模型:军医,实验室,lab_medic,我试着在lab_medic表上建立搜索功能。

这是我到目前为止所得到的:

lab_medic.rb

belongs_to :medic
belongs_to :lab

def self.search(search)
    if search
      q = "%#{search}%"

      joins(:medic).where('lab_medics.medic.anamnesa LIKE ? OR 
             medics.anamnesa LIKE ?',q,q)
    else
      scoped
    end
end

medic.rb

has_many :lab_medics
has_many :labs, :through => lab_medics

lab.rb

has_many :lab_medics
has_many :medics, :through => lab_medics

lab_medics_controller.rb

def search
    base_lab_medic = LabMedic.search(params[:search])
    @lab_medics_count = base_lab_medic.count
    @lab_medics = base_lab_medic.paginate(:page => params[:page])

    render :index
end

lab_medics / index.html.erb

    <div class="controls">
      <%= form_tag(search_lab_medics_path, :method => "get", id: "search-form") do %>
        <div class="input-group">       
          <%= text_field_tag :search, params[:search], placeholder: "Search Anamnesa", class: "form-control", id: "date" %>
           <span class="input-group-addon ">
             <i class="fa fa-search"></i>
           </span>     
        </div>
      <% end %>
   </div>

当我试图搜索anamnesa&#39;在lab_medic上,我有一个错误。如何修复我的搜索功能?

修改

lab_medics / index.html.erb

<table class="table table-striped table-hover" id="lab_medics">
  <thead>
    <tr>
      <th>Date</th>
      <th>Diagnosis</th>
      <th>Lab Test</th>
      <th>Price</th>
      <th>Result</th>
      <th>Action</th>
    </tr>
  </thead>
  <tbody id="lab_medics2">
    <% if @lab_medics.count > 0 %>
      <% @lab_medics.each do |lab_medic| %>
        <tr id="lab_medic-<%= lab_medic.id %>">
          <tr>
            <td><%= lab_medic.medic.date %></td>
            <td><%= lab_medic.medic.anamnesa %></td>
            <td><%= lab_medic.lab.name %></td>
            <td><%= lab_medic.lab.price %></td>
            <td><%= lab_medic.result %></td>
            <td>
              <div class="btn-group">
                <button class="btn btn-primary btn-sm dropdown-toggle" data-toggle="dropdown"><span class="fa fa-gear"></span> Action <span class="caret"></span></button>
                <ul class="dropdown-menu right-edge">
                  <li><%= link_to 'Edit', edit_lab_medic_path(lab_medic), remote: true, class: "separated-link" %></li>
                  <li><%= link_to 'Delete', lab_medic_path(lab_medic), method: :delete, data: { confirm: 'Are you sure?' }, class: "separated-link" %></li>
                </ul>
              </div>  
            </td>
          </tr>
        </tr>
      <% end %>
    <% else %>
      <tr>
        <td colspan="6" align="center">No medic data found!</td>
      </tr>
    <% end %>
  </tbody>
</table>

我想在lab_medic表上搜索医学模型中的anamnesa

2 个答案:

答案 0 :(得分:0)

由于medicssearch表的一列,您应该在Medic模型中定义def self.search(param) self.where("anamnesa like '%#{param}%'") end 方法,如下所示:

scope :search, -> (param) { where("anamnesa like '%#{param}%'") }

另一种方法是使用可能的范围:

search = medic_params[:search]
medics = Medic.search(search)

在您的控制器中,您应该致电:

medic_params[:search]

请注意我使用了强参数(return collection.find({f1:1, f2:1, f3:1}).count(); ),以避免SQL注入。您可以阅读更多相关信息strong parameters here

答案 1 :(得分:-1)

在这种情况下,您应该使用has_and_belongs_to_many协会