Hi guys! I am using will_paginate in my rails application. I want to merge these 2 lines in one: @cat_products1 = Product.find(:all, :conditions => {:category_id => params[:id]}) @cat_products2 = Product.paginate(:per_page => 5, :page => params [:page], :conditions => [''title like ?'' , "%# {params[:search]}%"], :order => ''title'') I want to paginate using the condition category_id=>params[:id] Any sollutions??? Thank u! Kostas L.
2009/9/19 Kostas L. <louposk-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>:> > Hi guys! > I am using will_paginate in my rails application. > I want to merge these 2 lines in one: > > @cat_products1 = Product.find(:all, :conditions => {:category_id > => params[:id]}) > @cat_products2 = Product.paginate(:per_page => 5, :page => params > [:page], > :conditions => [''title like ?'' , "%# > {params[:search]}%"], > :order => ''title'') > > I want to paginate using the condition category_id=>params[:id] > > Any sollutions???Can''t you just include the category_id in the conditions for the paginate call? Or am I missing something? Colin
I do this but i get a systax error. Maybe i''m doing something wrong. I wrote this: @cat_products = Product.paginate(:per_page => 5, :page => params [:page], :conditions => {[''title like ?'' , "%# {params[:search]}%"], :category_id => params[:id]}, :order => ''title'') Any ideas??? Thanx On Sep 20, 12:05 am, Colin Law <clan...-gM/Ye1E23mwN+BqQ9rBEUg@public.gmane.org> wrote:> 2009/9/19 Kostas L. <loup...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org>: > > > > > > > Hi guys! > > I am using will_paginate in my rails application. > > I want to merge these 2 lines in one: > > > @cat_products1 = Product.find(:all, :conditions => {:category_id > > => params[:id]}) > > @cat_products2 = Product.paginate(:per_page => 5, :page => params > > [:page], > > :conditions => [''title like ?'' , "%# > > {params[:search]}%"], > > :order => ''title'') > > > I want to paginate using the condition category_id=>params[:id] > > > Any sollutions??? > > Can''t you just include the category_id in the conditions for the > paginate call? Or am I missing something? > > Colin
> @cat_products = Product.paginate(:per_page => 5, :page => params > [:page], > :conditions => {[''title like ?'' , "%# > {params[:search]}%"], :category_id => params[:id]}, > :order => ''title'')You''re mixing 2 styles of defining conditions here. You should be able to do this: Product.paginate(:per_page => 5, :page => params[:page], :conditions => ["title LIKE ?", "%#{params [:search]}%", params[:id]], :order => ''title'') The :conditions array accepts either a string with ? placeholders followed by parameters in a list: ["title = ? AND name = ?, arg1, arg2] or a string with symbol placeholders followed by a hash: ["title = :title AND name = :name", {:title => "foo", :name => "bar"}] Hope that explains it and helps. Steve
2009/9/20 Stephen Bartholomew <steve-LWB5c3/XHbdBDgjK7y7TUQ@public.gmane.org>:> >> @cat_products = Product.paginate(:per_page => 5, :page => params >> [:page], >> :conditions => {[''title like ?'' , "%# >> {params[:search]}%"], :category_id => params[:id]}, >> :order => ''title'') > You''re mixing 2 styles of defining conditions here. You should be > able to do this: > > Product.paginate(:per_page => 5, > :page => params[:page], > :conditions => ["title LIKE ?", "%#{params > [:search]}%", params[:id]],Should that be :conditions => ["title LIKE ? AND category_id = ?", "%#{params> [:search]}%", params[:id]],Colin> :order => ''title'') > > The :conditions array accepts either a string with ? placeholders > followed by parameters in a list: > ["title = ? AND name = ?, arg1, arg2] > > or a string with symbol placeholders followed by a hash: > ["title = :title AND name = :name", {:title => "foo", :name => "bar"}] > > Hope that explains it and helps. > > Steve > > > >