0

I'm new at the rails world, i am trying to implement will_paginate with my Search model. The Search model is responsible for searching products.

My problem is that i can't figure out how to make will_paginate works with this two models, because the products are displayed by the search model and will_paginate only deals with controllers, i have spent a couple of days trying to make this work but without luck :(

This is my Search.rb

class Search < ActiveRecord::Base
def search_products
    products = Product.all

    products = products.where(["modelo LIKE ?","%#{modelo}%"]).order(created_at: :desc) if modelo.present?      
    products = products.where(["preco >= ?",min_price]) if min_price.present?   
    products = products.where(["preco <= ?",max_price]) if max_price.present?
    products = products.where(["troca LIKE ?","%#{troca}%"]) if troca.present?  
    products = products.where(["estado_id LIKE ?","%#{estado_id}%"]) 
    return  products
end

end

Search controller

    def show
    @search = Search.find(params[:id])
    @estado = Estado.all
    @products = @search.search_products.paginate(:page => params[:page], :per_page => 2).order('id DESC') 
end

and search show

<%= will_paginate @products%>

I think that is missing something ;S Please guys, give me a north of how to solve this problem ;S

ps: To make the Search i followed the Railscast tutorial.

0 Answers0