Forum: Ruby on Rails logic for a search method

Announcement (2017-05-07): is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see and for other Rails- und Ruby-related community platforms.
78d56bb0f91c899d40f1767b3040d825?d=identicon&s=25 bingo bob (bingobob)
on 2009-02-07 16:09

I'm trying to ask users for a name and a place and then give them a list
of all the names OR all the places OR the names in that place, if you
get me? This is my flawed attempt, I'm passing params correctly so it's
just the logic within the method I need to sort out, anyone mind
re-factoring it ?

Here's my code (wrong)
class School < ActiveRecord::Base

  has_many :reviews


      find(:all, :conditions => ['name LIKE ?', "%#{search_by_name}%"],
:limit => 100)

      find(:all, :conditions => ['place LIKE ?',
"%#{search_by_place}%"], :limit => 100)


47c3af2d56d44eb1d08a601c837a1262?d=identicon&s=25 alberto (Guest)
on 2009-02-07 22:09
(Received via mailing list)
A couple of things:
1) You want to make sure you protect against SLQ injection, so do not
pass the variables without escaping them. Rails does this for you when
it substitutes the ? in the find method.

2) You can use the code below to do what you want.

Hope that helps,

  conditions = []
  condition_values = []
  unless params[:name].blank?
    conditions << "name like ?"
    condition_values << "%#{params[:name]}%"
  unless params[:place].blank?
    conditions << "place like ?"
    condition_values << "%#{params[:place]}%"
  unless conditions.blank?
    self.find(:all, :conditions => [conditions.join(" AND "),
*condition_values], :limit => 100)
Da22eb702d7ab6c6c2f33a47ce9b1498?d=identicon&s=25 jammanbo (Guest)
on 2009-02-08 06:01
(Received via mailing list)
On Feb 7, 3:09 pm, bingo bob <> wrote:
>   has_many :reviews
> "%#{search_by_place}%"], :limit => 100)
>     end
>   end
> end
> --
> Posted via

You should take small steps, especially when you are still trying to
get to grips with the language.

I'll help you a little by pointing out that this can be a single
query. What you are trying to do is find stuff that match the
conditions 'name LIKE ? OR place LIKE ?'. So this should be a one line

I would also suggest you look up named_scope ( has a
screencast about it) which you could use here.

Good luck.
78d56bb0f91c899d40f1767b3040d825?d=identicon&s=25 bingo bob (bingobob)
on 2009-02-08 09:49

Alberto (or anyone)...

How do I pass in the params from the controller to use the method...

Something like

  # @schools =[:search_by_name],

But that gives me ..

wrong number of arguments (2 for 1)
47c3af2d56d44eb1d08a601c837a1262?d=identicon&s=25 alberto (Guest)
on 2009-02-09 02:01
(Received via mailing list)
the params in the Controller is a method that returns the hash coming
to the request. So you can pass the whole params method in the call.

@schools =
78d56bb0f91c899d40f1767b3040d825?d=identicon&s=25 bingo bob (bingobob)
on 2009-02-09 11:08
alberto wrote:
> the params in the Controller is a method that returns the hash coming
> to the request. So you can pass the whole params method in the call.
> @schools =

Many thanks, after staring at this for while I figured it.

Thanks for your help with this it works great and the idea behind it has
helped me a great deal.

Seems like this is something that I'll need to do a lot (pass the whole
params hash to some method (where it can be dealt with and return
This topic is locked and can not be replied to.