title: Rails: Search & Filter Books by Category
body: <p>I need some help please. I am building an <strong>advanced search feature</strong> for my digital library, and I would like to search books by their names or description and then by also filter the search results by the category which the books belongs.</p>
  
  <p>The database is structured this way: <strong>Books belongs sub-categories and categories, while sub-categories belongs to the categories only. But I want to filter by categories.</strong></p>
  
  <p>Let's say I have a book named <strong>Great Achievers</strong> and <strong>Great Developers</strong> and they belong to the categories <strong>Achievers</strong> and <strong>Developers</strong> respectively. I want the search to work this way: when I search using the keyword <strong>Great</strong> I would see the books <strong>Great Achievers</strong> and <strong>Great Developers</strong>, and when I filter by category <strong>Achievers</strong> I would see only <strong>Great Achievers</strong>.</p>
  
  <p>Right now my advanced search feature can search for books by their names and descriptions, but cannot filter the books by category in the search results.</p>
  
  <p><strong>This is the code for the book model</strong></p>
  
  <pre><code>class Book &lt; ApplicationRecord
    has_one_attached :upload
    belongs_to :category, required: false
    belongs_to :sub_category, required: false
  
    def self.search(keywords)
        if keywords
          where("name LIKE ? OR description LIKE ? OR author LIKE ?", "%#{keywords}%", "%#{keywords}%", "%#{keywords}%").order('id DESC')
        else
          order('id DESC') 
        end
      end
  end
  </code></pre>
  
  <p><strong>This is the code for the category model</strong></p>
  
  <pre><code>class Category &lt; ApplicationRecord
    has_many :sub_categories
    has_many :books
  end
  </code></pre>
  
  <p><strong>This is the code for the sub-category model</strong></p>
  
  <pre><code>class SubCategory &lt; ApplicationRecord
    has_many :books
    belongs_to :category, required: false
   end
  </code></pre>
  
  <p><strong>This is the truncated code for the books controller</strong></p>
  
  <pre><code>class BooksController &lt; ApplicationController
    def index
      @books = Book.search(params[:keywords]).paginate(:page =&gt; params[:page], :per_page =&gt; 9).order('created_at DESC')
    end
  
    private
      def set_book
        @book = Book.find(params[:id])
      end
  
      def book_params
        params.require(:book).permit(:name, :author, :description, :category_id, :sub_category_id, :new_sub_category_name, :upload, :keywords, :deep_keywords)
      end
  end
  </code></pre>
  
  <p><strong>This is the truncated code for the search feature on the books index view</strong></p>
  
  <pre><code>&lt;%= form_tag(books_path, method: :get) do %&gt;
      &lt;%= text_field_tag :keywords, params[:keywords] %&gt;
      &lt;%= collection_select :category, :id, Category.all.order('name ASC'), :id, :name,{include_blank: 'Select Category'} %&gt;
      &lt;%= submit_tag 'Search', name: nil %&gt;
  &lt;% end %&gt;
  </code></pre>
  
  <p><strong>I have attached a screenshot of my console log when a search action is done</strong><a href="https://i.stack.imgur.com/4W0L0.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/4W0L0.png" alt="enter image description here"></a>  </p>
  
  <p><strong>Please any form of assistance will be highly appreciated. Thank you.</strong></p>