Hi Everybody, I want to filter my table of data and am doing this by having the following code in my controller: def list model = @params[''model''] make = @params[''make''] @recipes = Recipe.find_all_by_make_and_model(make, model) end This is all well and good if the request parameters contain a value, but if they don''t, the controller automates a search for make = ''nil'' and obviously no results are returned. My ruby is very limited so if anyone could help it would be much appreciated. In fact if anybody knows of any articles for creating a dynamic filter, I would be very grateful if you could forward them to me. thanks, Oliver Owen
Not high tech, but it should work. def list if ! params[:make].nil? and params[:model].nil? @recipes = Recipe.find_all_by_make(params[:make]) elsif params[:make].nil? and ! params[:model].nil? @recipes = Recipe.find_all_by_model(params[:model]) else @recipes = Recipe.find_all_by_make_and_model(params[:make],params [:model]) end end P.S. params is a method call, not an attribute. -- -- Tom Mornini On Oct 11, 2005, at 2:01 AM, Oliver Owen wrote:> Hi Everybody, > > I want to filter my table of data and am doing this by having the > following code in my controller: > > def list > model = @params[''model''] > make = @params[''make''] > @recipes = Recipe.find_all_by_make_and_model(make, model) > end > > This is all well and good if the request parameters contain a > value, but if they don''t, the controller automates a search for > make = ''nil'' and obviously no results are returned. My ruby is very > limited so if anyone could help it would be much appreciated. > > In fact if anybody knows of any articles for creating a dynamic > filter, I would be very grateful if you could forward them to me. > > thanks, > Oliver Owen > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Thanks for your suggestion Tom and it will make a good plan B, but I was really trying to avoid a messy if statement (I failed to mention that I could have up to 6 other calls).>From: Tom Mornini <tmornini-W/9V78bTXriB+jHODAdFcQ@public.gmane.org> >Reply-To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >Subject: Re: [Rails] Search Filter using find_by >Date: Tue, 11 Oct 2005 02:14:20 -0700 > >Not high tech, but it should work. > >def list > if ! params[:make].nil? and params[:model].nil? > @recipes = Recipe.find_all_by_make(params[:make]) > elsif params[:make].nil? and ! params[:model].nil? > @recipes = Recipe.find_all_by_model(params[:model]) > else > @recipes = Recipe.find_all_by_make_and_model(params[:make],params >[:model]) > end >end > >P.S. params is a method call, not an attribute. > >-- >-- Tom Mornini > > >On Oct 11, 2005, at 2:01 AM, Oliver Owen wrote: > >>Hi Everybody, >> >>I want to filter my table of data and am doing this by having the >>following code in my controller: >> >>def list >> model = @params[''model''] >> make = @params[''make''] >> @recipes = Recipe.find_all_by_make_and_model(make, model) >>end >> >>This is all well and good if the request parameters contain a value, but >>if they don''t, the controller automates a search for make = ''nil'' and >>obviously no results are returned. My ruby is very limited so if anyone >>could help it would be much appreciated. >> >>In fact if anybody knows of any articles for creating a dynamic filter, I >>would be very grateful if you could forward them to me. >> >>thanks, >>Oliver Owen >> >> >>_______________________________________________ >>Rails mailing list >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails
Well then, you could use ''case'' instead. :-) -- -- Tom Mornini On Oct 11, 2005, at 2:23 AM, Oliver Owen wrote:> Thanks for your suggestion Tom and it will make a good plan B, but > I was really trying to avoid a messy if statement (I failed to > mention that I could have up to 6 other calls). > > > > >> From: Tom Mornini <tmornini-W/9V78bTXriB+jHODAdFcQ@public.gmane.org> >> Reply-To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> Subject: Re: [Rails] Search Filter using find_by >> Date: Tue, 11 Oct 2005 02:14:20 -0700 >> >> Not high tech, but it should work. >> >> def list >> if ! params[:make].nil? and params[:model].nil? >> @recipes = Recipe.find_all_by_make(params[:make]) >> elsif params[:make].nil? and ! params[:model].nil? >> @recipes = Recipe.find_all_by_model(params[:model]) >> else >> @recipes = Recipe.find_all_by_make_and_model(params >> [:make],params [:model]) >> end >> end >> >> P.S. params is a method call, not an attribute. >> >> -- >> -- Tom Mornini >> >> >> On Oct 11, 2005, at 2:01 AM, Oliver Owen wrote: >> >> >>> Hi Everybody, >>> >>> I want to filter my table of data and am doing this by having >>> the following code in my controller: >>> >>> def list >>> model = @params[''model''] >>> make = @params[''make''] >>> @recipes = Recipe.find_all_by_make_and_model(make, model) >>> end >>> >>> This is all well and good if the request parameters contain a >>> value, but if they don''t, the controller automates a search for >>> make = ''nil'' and obviously no results are returned. My ruby is >>> very limited so if anyone could help it would be much appreciated. >>> >>> In fact if anybody knows of any articles for creating a dynamic >>> filter, I would be very grateful if you could forward them to me. >>> >>> thanks, >>> Oliver Owen >>> >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails
On 11.10.2005, at 12.23, Oliver Owen wrote:> Thanks for your suggestion Tom and it will make a good plan B, but > I was really trying to avoid a messy if statement (I failed to > mention that I could have up to 6 other calls).Maybe you could build up a method name string depending on the params available and use send [1] to call the method. //jarkko [1] http://www.ruby-doc.org/core/classes/Object.html#M000998> > > > >> From: Tom Mornini <tmornini-W/9V78bTXriB+jHODAdFcQ@public.gmane.org> >> Reply-To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> Subject: Re: [Rails] Search Filter using find_by >> Date: Tue, 11 Oct 2005 02:14:20 -0700 >> >> Not high tech, but it should work. >> >> def list >> if ! params[:make].nil? and params[:model].nil? >> @recipes = Recipe.find_all_by_make(params[:make]) >> elsif params[:make].nil? and ! params[:model].nil? >> @recipes = Recipe.find_all_by_model(params[:model]) >> else >> @recipes = Recipe.find_all_by_make_and_model(params >> [:make],params [:model]) >> end >> end >> >> P.S. params is a method call, not an attribute. >> >> -- >> -- Tom Mornini >> >> >> On Oct 11, 2005, at 2:01 AM, Oliver Owen wrote: >> >> >>> Hi Everybody, >>> >>> I want to filter my table of data and am doing this by having >>> the following code in my controller: >>> >>> def list >>> model = @params[''model''] >>> make = @params[''make''] >>> @recipes = Recipe.find_all_by_make_and_model(make, model) >>> end >>> >>> This is all well and good if the request parameters contain a >>> value, but if they don''t, the controller automates a search for >>> make = ''nil'' and obviously no results are returned. My ruby is >>> very limited so if anyone could help it would be much appreciated. >>> >>> In fact if anybody knows of any articles for creating a dynamic >>> filter, I would be very grateful if you could forward them to me. >>> >>> thanks, >>> Oliver Owen >>> >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails > >-- Jarkko Laine http://jlaine.net http://odesign.fi _______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
How about something like this: def list @columns = []; # Not sure if these are necessary @values = []; for input in [''col1'',''col2'',''col3'',''col4'',''col5'',''col6''] unless param[input].nil? @columns << input @values << param[input] end end conditions = @column.collect { |c| c + " = ?" }.join '' and '' @recipes = Recipe.find(:all,:conditions => conditions,@values) end Code should be considered pseudo code, has not been compiled or tested. -- -- Tom Mornini On Oct 11, 2005, at 2:23 AM, Oliver Owen wrote:> Thanks for your suggestion Tom and it will make a good plan B, but > I was really trying to avoid a messy if statement (I failed to > mention that I could have up to 6 other calls). > > > > >> From: Tom Mornini <tmornini-W/9V78bTXriB+jHODAdFcQ@public.gmane.org> >> Reply-To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> Subject: Re: [Rails] Search Filter using find_by >> Date: Tue, 11 Oct 2005 02:14:20 -0700 >> >> Not high tech, but it should work. >> >> def list >> if ! params[:make].nil? and params[:model].nil? >> @recipes = Recipe.find_all_by_make(params[:make]) >> elsif params[:make].nil? and ! params[:model].nil? >> @recipes = Recipe.find_all_by_model(params[:model]) >> else >> @recipes = Recipe.find_all_by_make_and_model(params >> [:make],params [:model]) >> end >> end >> >> P.S. params is a method call, not an attribute. >> >> -- >> -- Tom Mornini >> >> >> On Oct 11, 2005, at 2:01 AM, Oliver Owen wrote: >> >> >>> Hi Everybody, >>> >>> I want to filter my table of data and am doing this by having >>> the following code in my controller: >>> >>> def list >>> model = @params[''model''] >>> make = @params[''make''] >>> @recipes = Recipe.find_all_by_make_and_model(make, model) >>> end >>> >>> This is all well and good if the request parameters contain a >>> value, but if they don''t, the controller automates a search for >>> make = ''nil'' and obviously no results are returned. My ruby is >>> very limited so if anyone could help it would be much appreciated. >>> >>> In fact if anybody knows of any articles for creating a dynamic >>> filter, I would be very grateful if you could forward them to me. >>> >>> thanks, >>> Oliver Owen >>> >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
Somewhere in the middle of that example, I switched to Perl! Just a few fixes... def list for input in [''col1'',''col2'',''col3'',''col4'',''col5'',''col6''] unless param[input].nil? columns << input values << param[input] end end conditions = columns.collect { |c| c + " = ?" }.join '' and '' @recipes = Recipe.find(:all,:conditions => [conditions,@values]) end -- -- Tom Mornini On Oct 11, 2005, at 2:48 AM, Tom Mornini wrote:> How about something like this: > > def list > @columns = []; # Not sure if these are necessary > @values = []; > for input in [''col1'',''col2'',''col3'',''col4'',''col5'',''col6''] > unless param[input].nil? > @columns << input > @values << param[input] > end > end > conditions = @column.collect { |c| c + " = ?" }.join '' and '' > @recipes = Recipe.find(:all,:conditions => conditions,@values) > end > > Code should be considered pseudo code, has not been compiled or > tested. > > -- -- Tom Mornini > > On Oct 11, 2005, at 2:23 AM, Oliver Owen wrote: > > >> Thanks for your suggestion Tom and it will make a good plan B, but >> I was really trying to avoid a messy if statement (I failed to >> mention that I could have up to 6 other calls). >> >> >> >> >> >>> From: Tom Mornini <tmornini-W/9V78bTXriB+jHODAdFcQ@public.gmane.org> >>> Reply-To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> Subject: Re: [Rails] Search Filter using find_by >>> Date: Tue, 11 Oct 2005 02:14:20 -0700 >>> >>> Not high tech, but it should work. >>> >>> def list >>> if ! params[:make].nil? and params[:model].nil? >>> @recipes = Recipe.find_all_by_make(params[:make]) >>> elsif params[:make].nil? and ! params[:model].nil? >>> @recipes = Recipe.find_all_by_model(params[:model]) >>> else >>> @recipes = Recipe.find_all_by_make_and_model(params >>> [:make],params [:model]) >>> end >>> end >>> >>> P.S. params is a method call, not an attribute. >>> >>> -- >>> -- Tom Mornini >>> >>> >>> On Oct 11, 2005, at 2:01 AM, Oliver Owen wrote: >>> >>> >>> >>>> Hi Everybody, >>>> >>>> I want to filter my table of data and am doing this by having >>>> the following code in my controller: >>>> >>>> def list >>>> model = @params[''model''] >>>> make = @params[''make''] >>>> @recipes = Recipe.find_all_by_make_and_model(make, model) >>>> end >>>> >>>> This is all well and good if the request parameters contain a >>>> value, but if they don''t, the controller automates a search for >>>> make = ''nil'' and obviously no results are returned. My ruby is >>>> very limited so if anyone could help it would be much appreciated. >>>> >>>> In fact if anybody knows of any articles for creating a dynamic >>>> filter, I would be very grateful if you could forward them to me. >>>> >>>> thanks, >>>> Oliver Owen >>>> >>>> >>>> _______________________________________________ >>>> Rails mailing list >>>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>> http://lists.rubyonrails.org/mailman/listinfo/rails >>>> >>>> >>>> >>> >>> _______________________________________________ >>> Rails mailing list >>> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>> http://lists.rubyonrails.org/mailman/listinfo/rails >>> >>> >> >> >> _______________________________________________ >> Rails mailing list >> Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >> http://lists.rubyonrails.org/mailman/listinfo/rails >> >> > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
I like the look of this and will give it a go. thanks.>From: Tom Mornini <tmornini-W/9V78bTXriB+jHODAdFcQ@public.gmane.org> >Reply-To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >Subject: Re: [Rails] Search Filter using find_by >Date: Tue, 11 Oct 2005 02:48:57 -0700 > >How about something like this: > >def list > @columns = []; # Not sure if these are necessary > @values = []; > for input in [''col1'',''col2'',''col3'',''col4'',''col5'',''col6''] > unless param[input].nil? > @columns << input > @values << param[input] > end > end > conditions = @column.collect { |c| c + " = ?" }.join '' and '' > @recipes = Recipe.find(:all,:conditions => conditions,@values) >end > >Code should be considered pseudo code, has not been compiled or tested. > >-- -- Tom Mornini > >On Oct 11, 2005, at 2:23 AM, Oliver Owen wrote: > >>Thanks for your suggestion Tom and it will make a good plan B, but I was >>really trying to avoid a messy if statement (I failed to mention that I >>could have up to 6 other calls). >> >> >> >> >>>From: Tom Mornini <tmornini-W/9V78bTXriB+jHODAdFcQ@public.gmane.org> >>>Reply-To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>To: rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>Subject: Re: [Rails] Search Filter using find_by >>>Date: Tue, 11 Oct 2005 02:14:20 -0700 >>> >>>Not high tech, but it should work. >>> >>>def list >>> if ! params[:make].nil? and params[:model].nil? >>> @recipes = Recipe.find_all_by_make(params[:make]) >>> elsif params[:make].nil? and ! params[:model].nil? >>> @recipes = Recipe.find_all_by_model(params[:model]) >>> else >>> @recipes = Recipe.find_all_by_make_and_model(params [:make],params >>>[:model]) >>> end >>>end >>> >>>P.S. params is a method call, not an attribute. >>> >>>-- >>>-- Tom Mornini >>> >>> >>>On Oct 11, 2005, at 2:01 AM, Oliver Owen wrote: >>> >>> >>>>Hi Everybody, >>>> >>>>I want to filter my table of data and am doing this by having the >>>>following code in my controller: >>>> >>>>def list >>>> model = @params[''model''] >>>> make = @params[''make''] >>>> @recipes = Recipe.find_all_by_make_and_model(make, model) >>>>end >>>> >>>>This is all well and good if the request parameters contain a value, >>>>but if they don''t, the controller automates a search for make = ''nil'' >>>>and obviously no results are returned. My ruby is very limited so if >>>>anyone could help it would be much appreciated. >>>> >>>>In fact if anybody knows of any articles for creating a dynamic >>>>filter, I would be very grateful if you could forward them to me. >>>> >>>>thanks, >>>>Oliver Owen >>>> >>>> >>>>_______________________________________________ >>>>Rails mailing list >>>>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>>http://lists.rubyonrails.org/mailman/listinfo/rails >>>> >>>> >>> >>>_______________________________________________ >>>Rails mailing list >>>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>>http://lists.rubyonrails.org/mailman/listinfo/rails >>> >> >> >>_______________________________________________ >>Rails mailing list >>Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >>http://lists.rubyonrails.org/mailman/listinfo/rails >> > >_______________________________________________ >Rails mailing list >Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org >http://lists.rubyonrails.org/mailman/listinfo/rails
On 11-okt-2005, at 11:01, Oliver Owen wrote:> > This is all well and good if the request parameters contain a > value, but if they don''t, the controller automates a search for > make = ''nil'' and obviously no results are returned. My ruby is very > limited so if anyone could help it would be much appreciated. > > In fact if anybody knows of any articles for creating a dynamic > filter, I would be very grateful if you could forward them to me.Duane Johnson has posted his "query" class to do just that (kind of query by example). Look in the archive (it was a couple of days ago if not yesterday). -- Julian "Julik" Tarkhanov