Forum: Ruby on Rails Searching multiple fields with conditions

Announcement (2017-05-07): www.ruby-forum.com is now read-only since I unfortunately do not have the time to support and maintain the forum any more. Please see rubyonrails.org/community and ruby-lang.org/en/community for other Rails- und Ruby-related community platforms.
Ec0f081d8feaacc12db2e0d3cab72f09?d=identicon&s=25 Heather White (hwhite)
on 2007-05-23 20:56
I am very new at RoR but am trying to tweak the code for the AJAX table
from http://dev.nozav.org/rails_ajax_table.html.

I have it all working the way I want except for one line of code:

conditions = ["firstname LIKE ?" , "%#{@params[:query]}%"] unless
@params[:query].nil?

I need this to look at multiple fields not just firstname.  Any idea how
I do this?
0ab6a5abab167b409f58d280dc59a229?d=identicon&s=25 Faisal N Jawdat (Guest)
on 2007-05-23 21:05
(Received via mailing list)
On May 23, 2007, at 2:56 PM, Heather White wrote:
> conditions = ["firstname LIKE ?" , "%#{@params[:query]}%"] unless
> @params[:query].nil?
>
> I need this to look at multiple fields not just firstname.  Any
> idea how
> I do this?

were you just doing = comparisons then you could extract a hash of
conditions from the params and pass that directly.  for what you're
doing you might look at using the ez_where plugin:

http://opensvn.csie.org/ezra/rails/plugins/dev/ez_where/

-faisal
2f9a03aa0fcfe945229cb6126eda2cb2?d=identicon&s=25 Philip Hallstrom (Guest)
on 2007-05-23 21:14
(Received via mailing list)
> I am very new at RoR but am trying to tweak the code for the AJAX table
> from http://dev.nozav.org/rails_ajax_table.html.
>
> I have it all working the way I want except for one line of code:
>
> conditions = ["firstname LIKE ?" , "%#{@params[:query]}%"] unless
> @params[:query].nil?
>
> I need this to look at multiple fields not just firstname.  Any idea how
> I do this?

unless params[:query].blank?
   query = "%#{@params[:query]}%"
   conditions = ["firstname LIKE ? OR lastname LIKE ?", query, query]
end

You can add as many OR's as you want as long as you keep adding more
query's to the end.

Your database may not like you if you do this, but that's another matter
:)

-philip
Ec0f081d8feaacc12db2e0d3cab72f09?d=identicon&s=25 Heather White (hwhite)
on 2007-05-23 21:28
Philip Hallstrom wrote:

>
> unless params[:query].blank?
>    query = "%#{@params[:query]}%"
>    conditions = ["firstname LIKE ? OR lastname LIKE ?", query, query]
> end
>


Thanks that did exactly what I needed.
6e9db38e16957cc51cf9cee9de399249?d=identicon&s=25 dasil003 (Guest)
on 2007-05-23 21:39
(Received via mailing list)
Also check out ActiveRecord::Extensions for more hash-based conditions
suck as field_like, field_contains, field_lt, field_gt, field_ne, etc.
(http://www.continuousthinking.com/tags/arext).

However, hash-based conditions are added together with AND rather than
OR, so it's not much use for this particular prooblem.
F86901feca747abbb5c6c020362ef2e7?d=identicon&s=25 zdennis (Guest)
on 2007-06-08 05:51
(Received via mailing list)
I am just about to release 0.7.0 for ActiveRecord::Extensions. I am
thinking of including OR functionality. Please email me privately if
you have comments or suggestions on this.

I am planning to have:
   Book.find :all, :conditions=>{ :name_like => "Star
Wars", :or_name_like=>"Star Trek" }

generate SQL like:
   name LIKE "Star Wars"OR name LIKE "Star Trek"

It's limitation is that it will not be able to group OR'd selectors,
but it provides basic OR capabilities. thought?

Zach
http://www.continuousthinking.com
This topic is locked and can not be replied to.