# Controller Search Strategy

**URL:** <https://www.ruby-forum.com/t/controller-search-strategy/183378>\
**Category:** Rails\
**Created:** [February 12, 2010, 6:13pm UTC](https://www.ruby-forum.com/t/controller-search-strategy/183378 "2010-02-12T18:13:12Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![sshefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sshefer](https://www.ruby-forum.com/u/sshefer)\
**Post date:** [February 12, 2010, 6:13pm UTC](https://www.ruby-forum.com/t/controller-search-strategy/183378/1 "2010-02-12T18:13:12Z")

</div>

I’m trying to create a search action for one of my controllers that  
can take in different params. Something I can use like an api call:

…/users/search?first\_name=john&org\_id=1

Any idea how I can start this? I’d like to have a clean “search”  
action but this is also copying the index action. I’m thinking that  
the action needs to serialize the params and apply them to a find but  
I’m not sure if this is the best way.

Thanks.

---

<div class="post-metadata">

**Author:** ![sshefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sshefer](https://www.ruby-forum.com/u/sshefer)\
**Post date:** [February 13, 2010, 11:45pm UTC](https://www.ruby-forum.com/t/controller-search-strategy/183378/2 "2010-02-13T23:45:30Z")

</div>

sshefer wrote:

> I’m trying to create a search action for one of my controllers that  
> can take in different params. Something I can use like an api call:
> 
> …/users/search?first\_name=john&org\_id=1
> 
> Any idea how I can start this? I’d like to have a clean “search”  
> action but this is also copying the index action. I’m thinking that  
> the action needs to serialize the params and apply them to a find but  
> I’m not sure if this is the best way.
> 
> Thanks.

Have you looked at searchlogic?

> **[GitHub - binarylogic/searchlogic: Searchlogic provides object based...](https://github.com/binarylogic/searchlogic)**
>
> Searchlogic provides object based searching, common named scopes, and other useful tools. - GitHub - binarylogic/searchlogic: Searchlogic provides object based searching, common named scopes, and o...

Sounds exactly what you are looking for.

Cheers  
Luke

---

<div class="post-metadata">

**Author:** ![sshefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sshefer](https://www.ruby-forum.com/u/sshefer)\
**Post date:** [February 14, 2010, 3:15am UTC](https://www.ruby-forum.com/t/controller-search-strategy/183378/3 "2010-02-14T03:15:02Z")

</div>

Shai S. wrote:

> Hey Luke,
> 
> Yeah - I’ve used search logic in the past. I’m trying to make search  
> calls  
> using ajax requests and as I understand it I can’t do that with search  
> logic  
> (I may be wrong).

Take what I say with a grain of salt, as I haven’t actually used  
Searchlogic… but as I understand it it basically just extends  
ActiveRecord to automagically add a bunch of convenience named scopes,  
and automatically uses those named scopes when you call search on the  
the model’s class? It really doesn’t mess with ActionController or  
ActionView at all besides adding a couple helper methods, so I can’t see  
how it makes ajax anymore difficult. Have you actually tried using  
Searchlogic with ajax and encountered problems? If so, what were they?

---

<div class="post-metadata">

**Author:** ![sshefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sshefer](https://www.ruby-forum.com/u/sshefer)\
**Post date:** [February 14, 2010, 3:05am UTC](https://www.ruby-forum.com/t/controller-search-strategy/183378/4 "2010-02-14T03:05:25Z")

</div>

Hey Luke,

Yeah - I’ve used search logic in the past. I’m trying to make search  
calls  
using ajax requests and as I understand it I can’t do that with search  
logic  
(I may be wrong).

---

<div class="post-metadata">

**Author:** ![sshefer](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/s/d9b06d/32.png) [@sshefer](https://www.ruby-forum.com/u/sshefer)\
**Post date:** [February 14, 2010, 5:07am UTC](https://www.ruby-forum.com/t/controller-search-strategy/183378/5 "2010-02-14T05:07:11Z")

</div>

Hi Paul,

I think you’re right - I took a second look at searchlogic (I haven’t  
used  
it for more than a year) and you can pass your search paramaters in the  
url  
pretty cleanly  
(search?search[http://www.google.com/url?sa=D&q=http://localhost:3000/activities/search%3Fsearch&usg=AFQjCNH06twZNMzjAxjE2ctwuV\_8X6BIkQ](http://www.google.com/url?sa=D&q=http://localhost:3000/activities/search%3Fsearch&usg=AFQjCNH06twZNMzjAxjE2ctwuV_8X6BIkQ)[order]=&search[created\_by\_id\_equals]=30&search[hidden]=1).  
I thought there was some method that would take an encoded url and  
decode  
it into the search params but you can just use it directly. My mistake.

Searchlogic it is! (Thanks Luke)

Thanks,

Shai
