Forum: Ruby on Rails Search Function

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.
1044b6709151f9e9c7233e05ba60af32?d=identicon&s=25 Inderpal Bhogal (pal)
on 2009-03-12 00:06
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!!!!
6ef8cb7cd7cd58077f0b57e4fa49a969?d=identicon&s=25 Brian Hogan (Guest)
on 2009-03-12 04:28
(Received via mailing list)
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 Bhogal
1044b6709151f9e9c7233e05ba60af32?d=identicon&s=25 Inderpal Bhogal (pal)
on 2009-03-12 10:52
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.
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-03-12 11:04
(Received via mailing list)
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 Bhogal <rails-mailing-list@andreas-s.net>
1044b6709151f9e9c7233e05ba60af32?d=identicon&s=25 Inderpal Bhogal (pal)
on 2009-03-12 12:30
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.
5f94b9b346c2aa648a80bc259978e5bc?d=identicon&s=25 Colin Law (Guest)
on 2009-03-12 12:43
(Received via mailing list)
Have you worked through the guides at http://guides.rubyonrails.org/?

I can highly recommend the book Agile Development with Rails (
http://www.pragprog.com/titles/rails3/agile-web-de...
)

The railscasts at 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 Bhogal <rails-mailing-list@andreas-s.net>
This topic is locked and can not be replied to.