I am a freshman when it comes to RoR, and I haven''t been able to figure this out. I need to figure out how to query records in my database using form check boxes (or radio buttons) then display them in that same instance. What would this look? I have been told that just putting a question mark after here, :conditions => field = ?, but what would the argument look like? Thanks in advance. -- Posted via http://www.ruby-forum.com/.
One handy way is to use named parameters in the ''conditions'' option: Something.find(:all, ["blah = :foo or honk = :beep or other = :foo", params]) If you name your form fields "foo" and "beep", such that params[:foo] and params[:beep] give you the values you''re looking for, that find() call will ''just work''. The documentation for ''conditions'' is at the top of this page: http://api.rubyonrails.com/classes/ActiveRecord/Base.html --Wilson. On 12/20/05, Arik <makingme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I am a freshman when it comes to RoR, and I haven''t been able to figure > this out. I need to figure out how to query records in my database using > form check boxes (or radio buttons) then display them in that same > instance. What would this look? > > I have been told that just putting a question mark after here, > :conditions => field = ?, but what would the argument look like? > > Thanks in advance. > > -- > Posted via http://www.ruby-forum.com/. > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >
This worked well for me (after some trial and error). Only slightly OT, how do you use these Named Variables and perform a ''like'' query? Using ''='' works no problem, but I receive strange results when I use something like: :conditions => ["FirstName like :firstname%", {:firstname => firstname}] ActiveRecord seems to put the ''%'' symbol outside of the quoted value, causing a SQL error. Grant On 12/20/05, Wilson Bilkovich <wilsonb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > One handy way is to use named parameters in the ''conditions'' option: > Something.find(:all, ["blah = :foo or honk = :beep or other = :foo", > params]) > If you name your form fields "foo" and "beep", such that params[:foo] > and params[:beep] give you the values you''re looking for, that find() > call will ''just work''. > > The documentation for ''conditions'' is at the top of this page: > http://api.rubyonrails.com/classes/ActiveRecord/Base.html > > --Wilson. > > On 12/20/05, Arik <makingme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I am a freshman when it comes to RoR, and I haven''t been able to figure > > this out. I need to figure out how to query records in my database using > > form check boxes (or radio buttons) then display them in that same > > instance. What would this look? > > > > I have been told that just putting a question mark after here, > > :conditions => field = ?, but what would the argument look like? > > > > Thanks in advance. > > > > -- > > Posted via http://www.ruby-forum.com/. > > _______________________________________________ > > 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 had to append the ''%'', something like ....{firstname => firstname}+''%'' The syntax isn''t correct, but you get the idea. Anybody have any better suggestions? - Nic. On 12/21/05, Grant Gelinas-Brown <ggelinasbrown-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> This worked well for me (after some trial and error). > > Only slightly OT, how do you use these Named Variables and perform a ''like'' > query? Using ''='' works no problem, but I receive strange results when I use > something like: > > :conditions => ["FirstName like :firstname%", {:firstname => firstname}] > > ActiveRecord seems to put the ''%'' symbol outside of the quoted value, > causing a SQL error. > > Grant > > > > On 12/20/05, Wilson Bilkovich <wilsonb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > One handy way is to use named parameters in the ''conditions'' option: > > Something.find(:all, ["blah = :foo or honk = :beep or other = :foo", > params]) > > If you name your form fields "foo" and "beep", such that params[:foo] > > and params[:beep] give you the values you''re looking for, that find() > > call will ''just work''. > > > > The documentation for ''conditions'' is at the top of this page: > > http://api.rubyonrails.com/classes/ActiveRecord/Base.html > > > > --Wilson. > > > > On 12/20/05, Arik <makingme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > I am a freshman when it comes to RoR, and I haven''t been able to figure > > > this out. I need to figure out how to query records in my database using > > > form check boxes (or radio buttons) then display them in that same > > > instance. What would this look? > > > > > > I have been told that just putting a question mark after here, > > > :conditions => field = ?, but what would the argument look like? > > > > > > Thanks in advance. > > > > > > -- > > > Posted via http://www.ruby-forum.com/. > > > _______________________________________________ > > > 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 > > >
Thanks for the tip there Nic. Worked for me. Like yourself, I''m curious if this is the only way, or if there is a ''rails way'' of doing this. Grant On 12/21/05, Nic Werner <nicwerner-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > I had to append the ''%'', something like ....{firstname => firstname}+''%'' > > The syntax isn''t correct, but you get the idea. Anybody have any > better suggestions? > > - Nic. > > On 12/21/05, Grant Gelinas-Brown <ggelinasbrown-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > This worked well for me (after some trial and error). > > > > Only slightly OT, how do you use these Named Variables and perform a > ''like'' > > query? Using ''='' works no problem, but I receive strange results when I > use > > something like: > > > > :conditions => ["FirstName like :firstname%", {:firstname => firstname}] > > > > ActiveRecord seems to put the ''%'' symbol outside of the quoted value, > > causing a SQL error. > > > > Grant > > > > > > > > On 12/20/05, Wilson Bilkovich <wilsonb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > One handy way is to use named parameters in the ''conditions'' option: > > > Something.find(:all, ["blah = :foo or honk = :beep or other = :foo", > > params]) > > > If you name your form fields "foo" and "beep", such that params[:foo] > > > and params[:beep] give you the values you''re looking for, that find() > > > call will ''just work''. > > > > > > The documentation for ''conditions'' is at the top of this page: > > > http://api.rubyonrails.com/classes/ActiveRecord/Base.html > > > > > > --Wilson. > > > > > > On 12/20/05, Arik <makingme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I am a freshman when it comes to RoR, and I haven''t been able to > figure > > > > this out. I need to figure out how to query records in my database > using > > > > form check boxes (or radio buttons) then display them in that same > > > > instance. What would this look? > > > > > > > > I have been told that just putting a question mark after here, > > > > :conditions => field = ?, but what would the argument look like? > > > > > > > > Thanks in advance. > > > > > > > > -- > > > > Posted via http://www.ruby-forum.com/. > > > > _______________________________________________ > > > > 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 >_______________________________________________ Rails mailing list Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org http://lists.rubyonrails.org/mailman/listinfo/rails
If you need that kind of thing, you''re perhaps better off modifying the params before passing them to the find(). e.g. params[:firstname] += ''%'' ..which will append a % sign to the end of the value of :firstname. On 12/21/05, Nic Werner <nicwerner-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I had to append the ''%'', something like ....{firstname => firstname}+''%'' > > The syntax isn''t correct, but you get the idea. Anybody have any > better suggestions? > > - Nic. > > On 12/21/05, Grant Gelinas-Brown <ggelinasbrown-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > This worked well for me (after some trial and error). > > > > Only slightly OT, how do you use these Named Variables and perform a ''like'' > > query? Using ''='' works no problem, but I receive strange results when I use > > something like: > > > > :conditions => ["FirstName like :firstname%", {:firstname => firstname}] > > > > ActiveRecord seems to put the ''%'' symbol outside of the quoted value, > > causing a SQL error. > > > > Grant > > > > > > > > On 12/20/05, Wilson Bilkovich <wilsonb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > One handy way is to use named parameters in the ''conditions'' option: > > > Something.find(:all, ["blah = :foo or honk = :beep or other = :foo", > > params]) > > > If you name your form fields "foo" and "beep", such that params[:foo] > > > and params[:beep] give you the values you''re looking for, that find() > > > call will ''just work''. > > > > > > The documentation for ''conditions'' is at the top of this page: > > > http://api.rubyonrails.com/classes/ActiveRecord/Base.html > > > > > > --Wilson. > > > > > > On 12/20/05, Arik <makingme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > I am a freshman when it comes to RoR, and I haven''t been able to figure > > > > this out. I need to figure out how to query records in my database using > > > > form check boxes (or radio buttons) then display them in that same > > > > instance. What would this look? > > > > > > > > I have been told that just putting a question mark after here, > > > > :conditions => field = ?, but what would the argument look like? > > > > > > > > Thanks in advance. > > > > > > > > -- > > > > Posted via http://www.ruby-forum.com/. > > > > _______________________________________________ > > > > 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 >
find :all, :conditions => ["name LIKE ?", "%#{params[:name]}%"] On 12/21/05, Wilson Bilkovich <wilsonb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > If you need that kind of thing, you''re perhaps better off modifying > the params before passing them to the find(). > e.g. > params[:firstname] += ''%'' > ..which will append a % sign to the end of the value of :firstname. > > On 12/21/05, Nic Werner <nicwerner-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > I had to append the ''%'', something like ....{firstname => firstname}+''%'' > > > > The syntax isn''t correct, but you get the idea. Anybody have any > > better suggestions? > > > > - Nic. > > > > On 12/21/05, Grant Gelinas-Brown <ggelinasbrown-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > This worked well for me (after some trial and error). > > > > > > Only slightly OT, how do you use these Named Variables and perform a > ''like'' > > > query? Using ''='' works no problem, but I receive strange results when > I use > > > something like: > > > > > > :conditions => ["FirstName like :firstname%", {:firstname => > firstname}] > > > > > > ActiveRecord seems to put the ''%'' symbol outside of the quoted value, > > > causing a SQL error. > > > > > > Grant > > > > > > > > > > > > On 12/20/05, Wilson Bilkovich <wilsonb-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > One handy way is to use named parameters in the ''conditions'' option: > > > > Something.find(:all, ["blah = :foo or honk = :beep or other = :foo", > > > params]) > > > > If you name your form fields "foo" and "beep", such that > params[:foo] > > > > and params[:beep] give you the values you''re looking for, that > find() > > > > call will ''just work''. > > > > > > > > The documentation for ''conditions'' is at the top of this page: > > > > http://api.rubyonrails.com/classes/ActiveRecord/Base.html > > > > > > > > --Wilson. > > > > > > > > On 12/20/05, Arik <makingme-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > > > I am a freshman when it comes to RoR, and I haven''t been able to > figure > > > > > this out. I need to figure out how to query records in my database > using > > > > > form check boxes (or radio buttons) then display them in that same > > > > > instance. What would this look? > > > > > > > > > > I have been told that just putting a question mark after here, > > > > > :conditions => field = ?, but what would the argument look like? > > > > > > > > > > Thanks in advance. > > > > > > > > > > -- > > > > > Posted via http://www.ruby-forum.com/. > > > > > _______________________________________________ > > > > > 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 > > > _______________________________________________ > 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