# Search Function

**URL:** <https://www.ruby-forum.com/t/search-function/162615>\
**Category:** Rails\
**Created:** [March 12, 2009, 12:06am UTC](https://www.ruby-forum.com/t/search-function/162615 "2009-03-12T00:06:24Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![pal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7bcc69/32.png) [@pal](https://www.ruby-forum.com/u/pal)\
**Post date:** [March 12, 2009, 12:06am UTC](https://www.ruby-forum.com/t/search-function/162615/1 "2009-03-12T00: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:** ![pal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7bcc69/32.png) [@pal](https://www.ruby-forum.com/u/pal)\
**Post date:** [March 12, 2009, 10:52am UTC](https://www.ruby-forum.com/t/search-function/162615/2 "2009-03-12T10: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:** ![pal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7bcc69/32.png) [@pal](https://www.ruby-forum.com/u/pal)\
**Post date:** [March 12, 2009, 11:04am UTC](https://www.ruby-forum.com/t/search-function/162615/3 "2009-03-12T11:04:31Z")

</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.

2009/3/12 Inderpal B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![pal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7bcc69/32.png) [@pal](https://www.ruby-forum.com/u/pal)\
**Post date:** [March 12, 2009, 12:30pm UTC](https://www.ruby-forum.com/t/search-function/162615/4 "2009-03-12T12:30:03Z")

</div>

hi colin,

does the join table have to be called categories\_stores. when this table  
was created i just called it categoriesstores (without the underscore\_).  
does that cause problems.

i have a button that when clicked it should look into stores table and  
match the store with the category it relates to and then takes the user  
to tht particular store page.

i have tried form\_for and tag methods but have been useless so far.  
i am quite new to ruby on rails and have been teaching myself for the  
last 3 months. is it possible for you provide sum sort of code that  
could help me.something which i can look at and implement into my  
system.

Many Thanks for replying Colin.

---

<div class="post-metadata">

**Author:** ![pal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7bcc69/32.png) [@pal](https://www.ruby-forum.com/u/pal)\
**Post date:** [March 12, 2009, 4:28am UTC](https://www.ruby-forum.com/t/search-function/162615/5 "2009-03-12T04:28:57Z")

</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?

On Wed, Mar 11, 2009 at 6:06 PM, Inderpal B.

---

<div class="post-metadata">

**Author:** ![pal](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/p/7bcc69/32.png) [@pal](https://www.ruby-forum.com/u/pal)\
**Post date:** [March 12, 2009, 12:43pm UTC](https://www.ruby-forum.com/t/search-function/162615/6 "2009-03-12T12:43:05Z")

</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 (

> **[Pragmatic Bookshelf: By Developers, For Developers](https://pragprog.com)**
>
> We improve the lives of professional developers. We create timely, practical books on classic and cutting-edge topics to help you learn and practice your craft, and accelerate your career. Come learn with us.

)

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

2009/3/12 Inderpal B. [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)
