title: Layering navbar simple search with Ransack for further scoping
body: <p>I have a search bar on my navigation header where users can search for the titles of articles. This will route them to the articles/browse.html.erb page where they can further scope the results down by categories and name of authors. </p>
  
  <p>I have followed <a href="https://rubyplus.com/articles/3381-Simple-Search-Form-in-Rails-5" rel="nofollow noreferrer">this</a> tutorial to do the navbar search. I have attempted to use Ransack to allow further filtering on the browse page. However, i'm having diffculty combining the 2 search methods with the following issues:</p>
  
  <p>1) The navbar search works fine and returns the correct results, but when i filter the results further again by category name, i get  <strong>EDIT <strike> 0 results </strike> ALL the results.</strong></p>
  
  <p>2) When i do the initial search on the browse page itself with the article title, ransack works. However, Ransack doesn't seem to work when i further scope the results down by category/author name. <strong>(EDIT: ransack works for filter by category with the below edit in browse.html.erb, but not for author name)</strong></p>
  
  <p><strong>browse.html.erb</strong></p>
  
  <pre><code>  &lt;%= search_form_for @q, url: browse_articles_path, method: :get do |f| %&gt;
      &lt;div class="form-group"&gt;
        &lt;div class = "row"&gt;
          &lt;div class="col-md-12 col-sm-12 col-xs-12"&gt;
            &lt;%= f.search_field :title_cont, placeholder: "Find Articles", class: "form-control" %&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
        &lt;div class ="row"&gt;
          &lt;div class="col-md-6 col-sm-6 col-xs-6"&gt;
            &lt;%= f.select :categories_name_cont, options_from_collection_for_select(Category.all, //EDIT change "id" to// "name", "name", @q.categories_name_eq), 
                                                                                  { :prompt =&gt; "Any Category" },
                                                                                  { class: "form-control" } %&gt;
          &lt;/div&gt;
          &lt;div class="col-md-6 col-sm-6 col-xs-6"&gt;
            &lt;%= f.search_field :users_name_cont, class: "form-control", placeholder: "Author" %&gt;
          &lt;/div&gt;
        &lt;/div&gt;
      &lt;/div&gt;
      &lt;div class="form-group"&gt;
        &lt;%= f.submit class: 'btn btn-primary btn-md' %&gt;
      &lt;/div&gt;
    &lt;% end %&gt;
  
    &lt;% if params[:search] %&gt;
      &lt;%= render @articles %&gt;
    &lt;% elsif params[:q] %&gt;
      &lt;%= render @articles_morefilter %&gt;
    &lt;% else %&gt;
      &lt;%= render @articles %&gt;
    &lt;% end %&gt;
  </code></pre>
  
  <p><strong>articles_controller.rb</strong></p>
  
  <pre><code>  def browse
      if params[:search]
        @articles = Article.search(params[:search])
        @categories = Category.joins(:articles).where('articles.title LIKE ?', "%#{params[:search]}%").uniq
      else
        @articles = Article.published.order("cached_votes_up DESC")
        @categories = Category.with_articles.order('name ASC').all
      end
  
        @q = Article.ransack(params[:q])
        @articles_morefilter = @q.result.includes(:categories, :users)
    end
  </code></pre>
  
  <p><strong>EDIT model: article.rb</strong></p>
  
  <pre><code>class Article &lt; ApplicationRecord
    belongs_to :user
    has_many :article_categories
    has_many :categories, through: :article_categories
  
    def self.search(search)
        where('title LIKE ?', "%#{search}%")
    end
  end
  </code></pre>
  
  <p><strong>routes.rb</strong> </p>
  
  <pre><code>  resources :articles do
      collection do
        get 'browse'
      end
    end
  </code></pre>
  
  <p><strong>schema:</strong></p>
  
  <pre><code>  create_table "articles", force: :cascade do |t|
      t.string "title"
      t.text "description"
      t.bigint "user_id"
    end
  
    create_table "users", force: :cascade do |t|
      t.string "name"
    end
  </code></pre>
  
  <p><strong>_navigation.html.erb</strong></p>
  
  <pre><code>&lt;%= form_tag(browse_articles_path, :method =&gt; 'get', :id =&gt; "articles_search") do %&gt;
    &lt;div class="form-group"&gt;
      &lt;%= text_field_tag :search, params[:search], placeholder: "Search", class: "form-control" %&gt;
    &lt;/div&gt;
    &lt;button type="submit" class="btn btn-default"&gt;Search&lt;/button&gt;
  &lt;% end %&gt;
  </code></pre>