code refactory on a small controller(thin controller fat model)

Viewed 168

I have a controller action that is doing product listing, pagination and some filters, like category(from a dropdown), title(from a text-field), stock(from a checkbox) This is my controller:

  class ProductsController < ApplicationController
  def index
    @products = Product.where(active:1).where("title LIKE ?","%#{params[:title]}%")
      if params[:stock]
        @products=@products.where("stock = 0")
      end
      if params[:category] 
        @products=@products.where("category_id LIKE ?","#{params[:category]}")
      end
    @products= @products.paginate(:page => params[:page])
    @categories= Category.all
  end

And my model is:

class Product < ActiveRecord::Base
  belongs_to :category
    ...some validations...
end

What could I change in order that my controller would become thinner? Thanks

4 Answers
Related