# Search Function

**URL:** <https://discuss.rubyonrails.org/t/search-function/40344>\
**Category:** rubyonrails-talk\
**Created:** [March 11, 2009, 11:06pm UTC](https://discuss.rubyonrails.org/t/search-function/40344 "2009-03-11T23:06:24Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [March 11, 2009, 11:06pm UTC](https://discuss.rubyonrails.org/t/search-function/40344/1 "2009-03-11T23:06:24Z")

</div>

Hi

I have two tables Stores and Categories

Relationship of these tables are: Many to Many Store can have many categories Category can also have many stores

Now in the stores table I have a store\_name and category\_id attributes that links the category table to the store table via its Category\_ID attribute.

The Category table only has category\_id and category\_description attributes.

I want to be able to perform a search on Categories index.html.erb page to find stores which fall into that particular category.

Therefore I need the search function to look into the stores table and match the category\_id to the category\_id in categories table, and return the store names with those categories.

I have tried some example but they don’t seem to be working. Can someone please help!!!!

---

<div class="post-metadata">

**Author:** ![bphogan](https://sea2.discourse-cdn.com/rubyonrails/user_avatar/discuss.rubyonrails.org/bphogan/32/4186_2.png) [@bphogan](https://discuss.rubyonrails.org/u/bphogan)\
**Post date:** [March 12, 2009, 3:28am UTC](https://discuss.rubyonrails.org/t/search-function/40344/2 "2009-03-12T03:28:10Z")

</div>

In general, a many-to-many relationship involves three tables. Can you describe how you've done this relationship with Rails? Did you use a has\_and\_belongs\_to\_many with a categories\_stories table? Or did you use a has\_many :through association with a different through table?

---

<div class="post-metadata">

**Author:** ![11175](https://avatars.discourse-cdn.com/v4/letter/1/8edcca/32.png) [@11175](https://discuss.rubyonrails.org/u/11175)\
**Post date:** [March 12, 2009, 9:52am UTC](https://discuss.rubyonrails.org/t/search-function/40344/3 "2009-03-12T09:52:25Z")

</div>

hi Brian,

thanx for repylin back, its v.much appriciated.

yes your absolutely right. i have used a has\_and\_belongs\_to\_many with a join table being created called categoriesstores. this join "categoriesstores" has the attributes category\_id and store\_id.

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [March 12, 2009, 10:03am UTC](https://discuss.rubyonrails.org/t/search-function/40344/4 "2009-03-12T10:03:20Z")

</div>

The join table should be called categories\_stores

Having setup the relationships and the tables, then, for example, if you have a store object from the database then you can use store.categories to get a collection of categories for that store.

---

<div class="post-metadata">

**Author:** ![Colin\_Law1](https://avatars.discourse-cdn.com/v4/letter/c/91b2a8/32.png) [@Colin\_Law1](https://discuss.rubyonrails.org/u/Colin_Law1)\
**Post date:** [March 12, 2009, 11:42am UTC](https://discuss.rubyonrails.org/t/search-function/40344/5 "2009-03-12T11:42:16Z")

</div>

Have you worked through the guides at [http://guides.rubyonrails.org/](http://guides.rubyonrails.org/)?

I can highly recommend the book Agile Development with Rails ([http://www.pragprog.com/titles/rails3/agile-web-development-with-rails-third-edition](http://www.pragprog.com/titles/rails3/agile-web-development-with-rails-third-edition))

The railscasts at [http://railscasts.com/](http://railscasts.com/) are excellent

A bit of googling will reveal numerous other guides and tutorials. Make sure any you work through are for rails 2 as otherwise you will get horribly confused when the tutorials don’t work.

Good luck

Colin
