title: Rails 4 search over several models in separate controller
body: <p>I'm trying to implement a complex search over a model in a separate controller. I have a pupils model. The whole app has a front page handled by a separate main_controller which has no model. The main_controller and its related index view are supposed to provide the front page and display data from several models.</p>
  
  <p>Now I want to search the model with several search criteria of different types. The search criteria are string comparisons, number comparisons and booleans (e.g. active, if true show only active pupils else show all pupils). <a href="http://railscasts.com/episodes/111-advanced-search-form" rel="nofollow">Railscast #111</a> showed how to create such a search based on a model and separate search controller. I created such a controller and it works fine. I am stuck with showing the relevant parts in my main/index.</p>
  
  <p>Here is the code:</p>
  
  <p>main/index.html.haml</p>
  
  <pre><code> - model_class = Adult
   - model_class = Pupil
   - model_class = MainSearch
  
  .page-header
  = render :partial =&gt; 'main_searches/form', :main_search =&gt; MainSearch.new
  </code></pre>
  
  <p>Just the call to the form here at the moment.</p>
  
  <p>models/main_search.rb</p>
  
  <pre><code>class MainSearch &lt; ActiveRecord::Base
  
    def pupils
      @pupils ||= find_pupils
    end
  
    private
      def find_pupils
        pupils = Pupil.order(:name_sur)
        pupils = pupils.where(id: id_search) if id_search.present?
        pupils = pupils.where("name_first like ?", "%#{name_first}%") if name_first.present?
        pupils = pupils.where("name_sur like ?", "%#{name_sur}%") if name_sur.present?
        pupils = pupils.where(active: "true") if active == true  #show only active or all
        pupils
      end
  end
  </code></pre>
  
  <p>This defines the search.</p>
  
  <p>controllers/main_searches_controller.rb</p>
  
  <pre><code>class MainSearchesController &lt; ApplicationController
    before_action :set_main_search, only: [:show, :update, :destroy]
  
    def show
      @main_search = MainSearch.find(params[:id])
    end
  
    def new
      @main_search = MainSearch.new
    end
  
    def create
      @main_search = MainSearch.new(main_search_params)
  
      if @main_search.save
        redirect_to root_path, notice: 'Main search was successfully created.'
      else
        render action: 'new'
      end
    end
  end
  </code></pre>
  
  <p>As shown in the railscast.</p>
  
  <p>views/main_searches/_form.html.haml</p>
  
  <pre><code>%h1 Advanced Search
  
    = form_for :main_search, :url =&gt; main_searches_path do |f|
      .field
        = f.label :id_search
        %br/
        = f.text_field :id_search
      [... ommitted some fields here ...]
      .field
        = f.label :active
        %br/
        = f.check_box :active, {}, true, false
  
  
    .actions= f.submit "Search"
  </code></pre>
  
  <p>Rendered in the new view.</p>
  
  <p>views/main_searches/_results.html.haml</p>
  
  <pre><code>%h1 Search Results
  
  .container-fluid
    .row-fluid
      .span4
  
        %table.table{style: "table-layout:fixed"}
          %thead
            %tr
              %th= "id"
              %th= "name_sur"
              %th= "name first"
              %th= "a"
        %div{style: "overflow-y:scroll; height: 200px"}
          %table.table.table-striped{style: "table-layout:fixed"}
            %tbody
              - @main_search.pupils.each do |pupil|
                %tr
                  %td= pupil.id
                  %td= link_to pupil.name_sur, pupil_path(pupil)
                  %td= pupil.name_first
                  %td= pupil.active
  </code></pre>
  
  <p>Displays the results.</p>
  
  <p>So basically everything works for one model as seen in the railscast. What I need
  now is to let the user handle everything in the main_controller somehow. At the moment I cant
  get the @main_search object passed to the _results.html.haml partial. What am I missing here?
  Or is this even the right way to do such a search?</p>
  
  <p>Thanks for your help in advance.</p>