Hi all, How do we add a conditions in a ActiveRecord find method when the parameter is available? Lets say I want to add this category conditions when the parameter from the client: Product.find(:all, :conditions => [ "c.id = ?", params[:catid]) Can we dynamically add that conditions to the find method when it is available? Or do I have to manually make an conditional statement when the parameter is available then give a different find method? Thanks in advance. -- If you can''t believe in God the chances are your God is too small. Read my blog: http://joshuajava.wordpress.com/ Follow me on twitter: http://twitter.com/jpartogi --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
I often do the brute-force approach: cond_string = [] cont_hash = {} if params[:search_name] cond_string << "(name ILIKE :name)" cond_hash[:name] = "%#{params[:search_name]}%" end if params[:search_address] cond_string << "(address ILIKE :name)" cond_hash[:address] = "%#{params[:search_address]}%" end conditions = cond_string.join(" AND ") Person.all :conditions => [ conditions, cond_hash ] --Michael On Sat, Feb 28, 2009 at 6:25 PM, Joshua Partogi <joshua.java-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> > Hi all, > > How do we add a conditions in a ActiveRecord find method when the > parameter is available? > > Lets say I want to add this category conditions when the parameter > from the client: > Product.find(:all, :conditions => [ "c.id = ?", params[:catid]) > > Can we dynamically add that conditions to the find method when it is > available? Or do I have to manually make an conditional statement when > the parameter is available then give a different find method? > > Thanks in advance. > > -- > If you can''t believe in God the chances are your God is too small. > > Read my blog: http://joshuajava.wordpress.com/ > Follow me on twitter: http://twitter.com/jpartogi > > > >--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Frederick Cheung
2009-Mar-01 02:53 UTC
Re: Adding the conditions when parameter is available
On Mar 1, 1:35 am, Michael Graff <skan.gryp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I often do the brute-force approach: > > cond_string = [] > cont_hash = {} > > if params[:search_name] > cond_string << "(name ILIKE :name)" > cond_hash[:name] = "%#{params[:search_name]}%" > end > if params[:search_address] > cond_string << "(address ILIKE :name)" > cond_hash[:address] = "%#{params[:search_address]}%" > end > > conditions = cond_string.join(" AND ") > Person.all :conditions => [ conditions, cond_hash ] >The merge_conditions method (which was protected in previous versions of rails) and the scoped named_scope are also handy for this sort of thing. Fred.> --Michael > > On Sat, Feb 28, 2009 at 6:25 PM, Joshua Partogi <joshua.j...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote: > > > Hi all, > > > How do we add a conditions in a ActiveRecord find method when the > > parameter is available? > > > Lets say I want to add this category conditions when the parameter > > from the client: > > Product.find(:all, :conditions => [ "c.id = ?", params[:catid]) > > > Can we dynamically add that conditions to the find method when it is > > available? Or do I have to manually make an conditional statement when > > the parameter is available then give a different find method? > > > Thanks in advance. > > > -- > > If you can''t believe in God the chances are your God is too small. > > > Read my blog:http://joshuajava.wordpress.com/ > > Follow me on twitter:http://twitter.com/jpartogi--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Joshua Partogi
2009-Mar-01 03:28 UTC
Re: Adding the conditions when parameter is available
Wow this is a very clean approach. Trying this one out. :-) Thanks heaps. On Mar 1, 12:35 pm, Michael Graff <skan.gryp...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> I often do the brute-force approach: > > cond_string = [] > cont_hash = {} > > if params[:search_name] > cond_string << "(name ILIKE :name)" > cond_hash[:name] = "%#{params[:search_name]}%" > end > if params[:search_address] > cond_string << "(address ILIKE :name)" > cond_hash[:address] = "%#{params[:search_address]}%" > end > > conditions = cond_string.join(" AND ") > Person.all :conditions => [ conditions, cond_hash ]--~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Joshua Partogi wrote:> Wow this is a very clean approach. Trying this one out. :-)>> cond_string = [] >> cont_hash = {} >> >> if params[:search_name] >> cond_string << "(name ILIKE :name)" >> cond_hash[:name] = "%#{params[:search_name]}%" >> endIt also prevents SQL injection attacks: http://imgs.xkcd.com/comics/exploits_of_a_mom.png --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk+unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---