Dear All, Can you guide me how can i implement a search function in my simple Bookmark program. In my Bookmark I have "title" and "url", "description". What I want to do is "search_by" "title". many thanks in advance, regards, koko
Salai Koko wrote:> Dear All, > > Can you guide me how can i implement a search function in my simple > Bookmark program. In my Bookmark I have "title" and "url", > "description". What I want to do is "search_by" "title". > > many thanks in advance, > > regards, > kokoview: <%form_for :search, :url => {:action => ''search'', :controller=>''aaa''} do |f| %> Search: <%= f.text_field :query %> <%= submit_tag "Search" %> <% end %> Controller: def search t = params[:search][:query] @q = Model.find(:all, :conditions=>["title like ? AND url like ? AND description like ?", t,t,t"]) end -- Posted via http://www.ruby-forum.com/.
Wouldn''t you want to use an OR between the conditions here? Otherwise you''re going to only find stuff where the title, url and description are all the same. What are the odds of that? On Jul 20, 11:36 pm, Rails List <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> Controller: > > def search > t = params[:search][:query] > @q = Model.find(:all, :conditions=>["title like ? AND url like ? AND > description like ?", t,t,t"]) > end
Railscasts has a nice screencast called Simple Search Form Enjoy! Bryan
Zach Karpinski wrote:> Wouldn''t you want to use an OR between the conditions here? Otherwise > you''re going to only find stuff where the title, url and description > are all the same. What are the odds of that? > > On Jul 20, 11:36�pm, Rails List <rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org>Yep!. It should be OR -- Posted via http://www.ruby-forum.com/.
Thankyou very much.> def search > t = params[:search][:query] > @q = Model.find(:all, :conditions=>["title like ? AND url like ? AND > description like ?", t,t,t"]) > endCan you explain me this part again? description like ?", t,t,t"]) what is the t,t,t " in this code ? regards, koko On Tue, Jul 21, 2009 at 6:36 AM, Rails List<rails-mailing-list-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote:> > Salai Koko wrote: >> Dear All, >> >> Can you guide me how can i implement a search function in my simple >> Bookmark program. In my Bookmark I have "title" and "url", >> "description". What I want to do is "search_by" "title". >> >> many thanks in advance, >> >> regards, >> koko > > view: > > <%form_for :search, :url => {:action => ''search'', :controller=>''aaa''} do > |f| %> > Search: > <%= f.text_field :query %> > <%= submit_tag "Search" %> > <% end %> > > Controller: > > def search > t = params[:search][:query] > @q = Model.find(:all, :conditions=>["title like ? AND url like ? AND > description like ?", t,t,t"]) > end > -- > Posted via http://www.ruby-forum.com/. > > > >
The variable t was assigned to the value in params[:search][:query]. It just makes the example less terse and easier to type out. The value in t gets substituted for those ? marks. The ? mark method is better than putting them directly into the conditions string. This is because Rails will scrub the value in t to make sure it doesn''t contain any attempts to send malicious SQL directly into your database. Imagine if you will that I sent my query as this: "; delete all from models; You probably want to do a search on a partial match since it is unlikely the user will exactly type in a title, url or description. This means you''ll need to send MySQL some % signs to indicate variables. Something like this: @q = Model.find(:all, :conditions=>["title like ? OR url like ? OR description like ?", "%#{t}%", "%#{t}%", "%#{t}%"]) The "%#{t}%" tells MySQL to search for matches where a title, url, or description contains the search term. Without them MySQL will try to match each field exactly to the submitted query. Again, unlikely that your users will be providing search terms that match that precisely. On Jul 22, 10:14 am, salai <saya...-Re5JQEeQqe8AvxtiuMwx3w@public.gmane.org> wrote:> Thankyou very much. > > > def search > > t = params[:search][:query] > > @q = Model.find(:all, :conditions=>["title like ? AND url like ? AND > > description like ?", t,t,t"]) > > end > > Can you explain me this part again? > > description like ?", t,t,t"]) > > what is the t,t,t " in this code ? > > regards, > > koko > > On Tue, Jul 21, 2009 at 6:36 AM, Rails > > > > List<rails-mailing-l...-ARtvInVfO7ksV2N9l4h3zg@public.gmane.org> wrote: > > > Salai Koko wrote: > >> Dear All, > > >> Can you guide me how can i implement a search function in my simple > >> Bookmark program. In my Bookmark I have "title" and "url", > >> "description". What I want to do is "search_by" "title". > > >> many thanks in advance, > > >> regards, > >> koko > > > view: > > > <%form_for :search, :url => {:action => ''search'', :controller=>''aaa''} do > > |f| %> > > Search: > > <%= f.text_field :query %> > > <%= submit_tag "Search" %> > > <% end %> > > > Controller: > > > def search > > t = params[:search][:query] > > @q = Model.find(:all, :conditions=>["title like ? AND url like ? AND > > description like ?", t,t,t"]) > > end > > -- > > Posted viahttp://www.ruby-forum.com/.
Hi, first of all I AM A TOTAL NEWBIE so any help would be deeply appreciated. I need to do a search like this one only with exact terms (no like comparison) , but my real problem is that I need to do it from Flex 3, and I have no idea how can I modify the controller to do this and how to configure the HTTPService so ruby understands that I need it to execute the search def :/, I really need help on this one, please respond. Thanks in advance for any help.