# Keyword search

**URL:** <https://www.ruby-forum.com/t/keyword-search/139272>\
**Category:** Rails\
**Created:** [June 2, 2008, 2:48pm UTC](https://www.ruby-forum.com/t/keyword-search/139272 "2008-06-02T14:48:23Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [June 2, 2008, 2:48pm UTC](https://www.ruby-forum.com/t/keyword-search/139272/1 "2008-06-02T14:48:23Z")

</div>

I’m having trouble trying to implement a basic keyword search in my  
app. I’m trying to search through multiple tables. I m using a vendor  
plugin for the conditions(which should not effect the search).  
The association in the models is as following

The furni model contains this association to the raider model:

belongs\_to :raider, :foreign\_key =\> “original\_raider\_id”

The controller call

unless params[:search].blank?  
@furni\_pages, @furnii = paginate :furnii,  
:conditions =\> Furni.conditions\_by\_like(params[:search]),  
:per\_page =\> 10,  
:order =\> order\_from\_params

So what I’m trying to do is to have the search go through the raiders  
table as well. I do not know if it’s possible. I tried joins with no  
luck. Hope some one can give me a pointer. Thanks

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [June 2, 2008, 2:53pm UTC](https://www.ruby-forum.com/t/keyword-search/139272/2 "2008-06-02T14:53:29Z")

</div>

the include statement should work:

@furni\_pages, @furnii = paginate :furnii,  
:include =\> “raiders”,  
:conditions =\> Furni.conditions\_by\_like(params[:search]),  
:per\_page =\> 10,  
:order =\> order\_from\_params

then you could enhance your conditions with something like:  
“raiders.column\_name=‘whatever’”

important point is, that if you use include, you’ll have to  
specify the table name for all fields that exist in both tables  
(eg created\_at)

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [June 2, 2008, 3:12pm UTC](https://www.ruby-forum.com/t/keyword-search/139272/3 "2008-06-02T15:12:55Z")

</div>

Thorsten M. wrote:

> the include statement should work:
> 
> @furni\_pages, @furnii = paginate :furnii,  
> :include =\> “raiders”,  
> :conditions =\> Furni.conditions\_by\_like(params[:search]),  
> :per\_page =\> 10,  
> :order =\> order\_from\_params
> 
> then you could enhance your conditions with something like:  
> “raiders.column\_name=‘whatever’”
> 
> important point is, that if you use include, you’ll have to  
> specify the table name for all fields that exist in both tables  
> (eg created\_at)

how do I incorporate the “raiders.column\_name=‘whatever’” in the  
existing conditions?

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [June 2, 2008, 3:19pm UTC](https://www.ruby-forum.com/t/keyword-search/139272/4 "2008-06-02T15:19:49Z")

</div>

> how do I incorporate the “raiders.column\_name=‘whatever’” in the  
> existing conditions?

This may depend on what exactly this  
Furni.conditions\_by\_like(params[:search]) returns.

in general:

:conditions =\> { “raiders.column\_name” =\> “something”,  
Furni.conditions\_by\_like(params[:search]) })

may work, if your method returns a simple conditions string.  
If it returns the conditions in Array or Hash form, you may have to  
merge it with the additional conditions for raiders.

---

<div class="post-metadata">

**Author:** ![staypufd](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/ecd19e/32.png) [@staypufd](https://www.ruby-forum.com/u/staypufd)\
**Post date:** [June 2, 2008, 5:17pm UTC](https://www.ruby-forum.com/t/keyword-search/139272/5 "2008-06-02T17:17:39Z")

</div>

Thorsten M. wrote:

> > how do I incorporate the “raiders.column\_name=‘whatever’” in the  
> > existing conditions?
> 
> This may depend on what exactly this  
> Furni.conditions\_by\_like(params[:search]) returns.
> 
> in general:
> 
> :conditions =\> { “raiders.column\_name” =\> “something”,  
> Furni.conditions\_by\_like(params[:search]) })
> 
> may work, if your method returns a simple conditions string.  
> If it returns the conditions in Array or Hash form, you may have to  
> merge it with the additional conditions for raiders.

Thanx for your help, but it does not work. The conditions\_by\_like  
function is a vendor plug in and it performs a generic search(below). I  
do not know what I’m doing wrong.

def conditions\_by\_like(value, \*columns)  
columns = self.user\_columns if columns.size==0  
columns = columns[0] if columns[0].kind\_of?(Array)  
conditions = columns.map {|c|  
c = c.name if c.kind\_of?  
ActiveRecord::ConnectionAdapters::Column  
"`#{c}` LIKE " +  
ActiveRecord::Base.connection.quote("%#{value}%")  
}.join(" OR ")  
end

CONTROLLER CALL

def search  
unless params[:search].blank?  
@furni\_pages, @furnii = paginate :furnii,  
:include =\> “raider”,  
#:conditions =\> {“raiders.column\_name” =\> “email”,  
Furni.conditions\_by\_like(params[:search])},  
:conditions =\> {“raiders.column\_name=‘email’”,  
Furni.conditions\_by\_like(params[:search])},  
:per\_page =\> 10,  
:order =\> order\_from\_params  
else  
index  
end  
render :partial=\>‘furnilist’, :layout=\>false  
end
