Hi, First, Rails is excellent. Thank you! It''s been a steep learning curve for me, but I am starting to feel like it''s becoming more familiar now. I have the following code: class PeopleController < ApplicationController layout ''layouts/default'' scaffold :person def index end def list @people = Person.find_all(["given_name = ? AND surname = ?", @params[''person''][''given_name''], @params[''person''][''surname'']]) end end Does rails already provide a way to make this work if only one parameter is supplied, and not both? James James
On Fri, 2005-01-28 at 13:44 -0700, James Earl wrote:> Hi, > > First, Rails is excellent. Thank you! It''s been a steep learning curve for > me, but I am starting to feel like it''s becoming more familiar now. > > I have the following code: > > class PeopleController < ApplicationController > layout ''layouts/default'' > scaffold :person > > def index > end > > def list > @people = Person.find_all(["given_name = ? AND surname = ?", > @params[''person''][''given_name''], @params[''person''][''surname'']]) > end > end > > Does rails already provide a way to make this work if only one parameter is > supplied, and not both?Wouldn''t your problem be that if you are coming back from a form that the value could be empty. Empty can very well be a valid value on some forms. How about def list args = [] sql = [] if @params[''person''][''given_name''].length args.push @params[''person''][''given_name''] sql.push "given_name = ?" end if @params[''person''][''surname''].length args.push @params[''person''][''surname''] sql.push "surname = ?" end @people = Person.find_all( [ sql.join(" and "), args].flatten ) end -- Steven Critchfield <critch-wQLwMjUOumVBDgjK7y7TUQ@public.gmane.org>
> How about > def list > args = [] > sql = [] > if @params[''person''][''given_name''].length > args.push @params[''person''][''given_name''] > sql.push "given_name = ?" > end > if @params[''person''][''surname''].length > args.push @params[''person''][''surname''] > sql.push "surname = ?" > end > > @people = Person.find_all( [ sql.join(" and "), args].flatten ) > endHow about: Person.find_all( [@params[''person''].keys.collect{ |k| "#{k}=?" }.join('' and ''), *@params[''person''].values ]) :) -- Tobi http://www.hieraki.org - Open source book authoring http://blog.leetsoft.com - Technical weblog
On January 28, 2005 8:10 pm, Tobias Luetke wrote:> > How about > > def list > > args = [] > > sql = [] > > if @params[''person''][''given_name''].length > > args.push @params[''person''][''given_name''] > > sql.push "given_name = ?" > > end > > if @params[''person''][''surname''].length > > args.push @params[''person''][''surname''] > > sql.push "surname = ?" > > end > > > > @people = Person.find_all( [ sql.join(" and "), args].flatten ) > > end > > How about: > > Person.find_all( [@params[''person''].keys.collect{ |k| "#{k}=?" > }.join('' and ''), *@params[''person''].values ]) > > :)I knew there must be a way to do this on one line with Ruby! :) Thanks
On Fri, 2005-01-28 at 22:10 -0500, Tobias Luetke wrote:> > How about > > def list > > args = [] > > sql = [] > > if @params[''person''][''given_name''].length > > args.push @params[''person''][''given_name''] > > sql.push "given_name = ?" > > end > > if @params[''person''][''surname''].length > > args.push @params[''person''][''surname''] > > sql.push "surname = ?" > > end > > > > @people = Person.find_all( [ sql.join(" and "), args].flatten ) > > end > > How about: > > Person.find_all( [@params[''person''].keys.collect{ |k| "#{k}=?" > }.join('' and ''), *@params[''person''].values ])I don''t see what is taking care of the empty variable here. -- Steven Critchfield <critch-wQLwMjUOumVBDgjK7y7TUQ@public.gmane.org>
On January 28, 2005 8:10 pm, Tobias Luetke wrote:> Person.find_all( [@params[''person''].keys.collect{ |k| "#{k}=?" > }.join('' and ''), *@params[''person''].values ])Upon trying this, I realize this isn''t quite what I''m after. Say you enter a given name of ''John'', and leave the surname blank. This will generate an SQL query like WHERE given_name = ''John'' AND surname = '''', and will therefore (in my situation) list all people with the firstname ''Tobias'' and that have a surname that is empty. I know the following is ugly (I''m a newbie okay!), but this is the behavior I''m after. :) def list @args = String.new if @params[''person''] for param in @params[''person''] if !param[1].empty? @args.concat " AND " if !@args.empty? @args.concat "#{param[0]} LIKE ''#{param[1]}''" end end if !@args.empty? @people = Person.find_all(@args) else render_action ''index'' end else render_action ''index'' end end
On January 29, 2005 9:15 am, Steven Critchfield wrote:> On Fri, 2005-01-28 at 22:10 -0500, Tobias Luetke wrote: > > > How about > > > def list > > > args = [] > > > sql = [] > > > if @params[''person''][''given_name''].length > > > args.push @params[''person''][''given_name''] > > > sql.push "given_name = ?" > > > end > > > if @params[''person''][''surname''].length > > > args.push @params[''person''][''surname''] > > > sql.push "surname = ?" > > > end > > > > > > @people = Person.find_all( [ sql.join(" and "), args].flatten ) > > > end > > > > How about: > > > > Person.find_all( [@params[''person''].keys.collect{ |k| "#{k}=?" > > }.join('' and ''), *@params[''person''].values ]) > > I don''t see what is taking care of the empty variable here.Exactly. Here''s another possibility using what I''ve learned from both your examples: def list sql = Array.new args = Array.new @params[''person''].each do |key,value| if !value.empty? sql.push "#{key} = ?" args.push value end end @people = Person.find_all([sql.join('' AND ''), *args]) end I still need to add handling for the case when no arguments are submitted. James
On Sat, 2005-01-29 at 10:28 -0700, James Earl wrote:> Exactly. Here''s another possibility using what I''ve learned from both your > examples: > > def list > sql = Array.newsql = ["1=?"]> args = Array.newargs = [1]> > @params[''person''].each do |key,value| > if !value.empty? > sql.push "#{key} = ?" > args.push value > end > end > > @people = Person.find_all([sql.join('' AND ''), *args]) > end > > I still need to add handling for the case when no arguments are submitted.Check the inline diff -- Steven Critchfield <critch-wQLwMjUOumVBDgjK7y7TUQ@public.gmane.org>
On January 29, 2005 10:37 am, Steven Critchfield wrote:> On Sat, 2005-01-29 at 10:28 -0700, James Earl wrote: > > Exactly. Here''s another possibility using what I''ve learned from both > > your examples: > > > > def list > > sql = Array.new > > sql = ["1=?"] > > > args = Array.new > > args = [1] > > > @params[''person''].each do |key,value| > > if !value.empty? > > sql.push "#{key} = ?" > > args.push value > > end > > end > > > > @people = Person.find_all([sql.join('' AND ''), *args]) > > end > > > > I still need to add handling for the case when no arguments are > > submitted. > > Check the inline diffThat does the trick. Thanks! :)