Hi all, How do I do a search using ActionRecord? I simply want the following SQL: select * from product where title like ''%SEARCH_STRING%''; Is there a way to do this with AR without dropping down to SQL? i.e. something like this would be ideal: Product.search_by_title(searchString, "title", {:limit_to=>25}) Thanks! --Dave.
Model.find :all, :conditions => ["title like ?", "%SEARCH_STRING%"], :limit => 25 On 5/22/05, David Teare <dteare-LXYvB7aEQDJCkLs28/y7ANBPR1lH4CV8@public.gmane.org> wrote:> Hi all, > > How do I do a search using ActionRecord? I simply want the following SQL: > > select * from product where title like ''%SEARCH_STRING%''; > > Is there a way to do this with AR without dropping down to SQL? i.e. > something like this would be ideal: > > Product.search_by_title(searchString, "title", {:limit_to=>25}) > > Thanks! > --Dave. > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >-- Tobi http://www.snowdevil.ca - Snowboards that don''t suck http://www.hieraki.org - Open source book authoring http://blog.leetsoft.com - Technical weblog
If you''d like a more flexible yet simpler solution, you can install this addition to ActiveRecord that I made: http://wiki.rubyonrails.com/rails/show/TextSearch This will give you the capability, among other things, to search all text fields in your model like this: matching_products = Product.search "shoe" Duane Johnson (canadaduane) On May 22, 2005, at 6:55 AM, David Teare wrote:> Hi all, > > How do I do a search using ActionRecord? I simply want the > following SQL: > > select * from product where title like ''%SEARCH_STRING%''; > > Is there a way to do this with AR without dropping down to SQL? > i.e. something like this would be ideal: > > Product.search_by_title(searchString, "title", {:limit_to=>25}) > > Thanks! > --Dave. > > > _______________________________________________ > Rails mailing list > Rails-1W37MKcQCpIf0INCOvqR/iCwEArCW2h5@public.gmane.org > http://lists.rubyonrails.org/mailman/listinfo/rails >