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 <[email protected]> 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<[email protected]> 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/.
--~--~---------~--~----~------------~-------~--~----~
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 [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---