Topic: Making a list of categories and connecting it

I've got a category field for products in my table.  Can someone give some starting hints on how I would make all the different categories populate a list and be linked to a page containing only items from that category.  I've been reading up on searches thinking that may be the way to go but it's starting to not look like it.  I'm not looking for a complete answer since I am trying to learn this(at least yet we'll see how hard it is) but I would like some help on where to begin.  thanks!

Re: Making a list of categories and connecting it

What you want to do is create two separate models: Category and Product.

You'll be using just one controller: CategoryController

You'll be using two methods in the controller: 'index' will show the list of categories and 'show' will show all the products in a category.

A product should 'belong_to' a category.  Then, when you want to make a list of categories you just retrieve them all from the database with @categories = Category.find(:all).  To make individual pages that show all the products you'll use the 'show' method and it will be able to display all the products under any category.

Hint: use Product.find_all_by_category_id(params[:id]) to find all the products that are in a given category.

If you want the code for it feel free to ask.  I didn't want to give everything away because it wasn't that long ago that I was in your shoes and I know how beneficial it is to have to figure things out :-)

Re: Making a list of categories and connecting it

Well if you could post the working code so I can see what bugs I still need to fix I'd appreciate it.....I think I got decently far on my own (well on my own with your initial help), but now I'm just getting frustrated cause I'm not sure what I'm leaving out.  Thanks again

Re: Making a list of categories and connecting it

would it be better to make "categories" a separate table?  Then have something like id, category_name, and product_id (foreign key).  Then in the model do the has_many/belongs_to for the two.

Re: Making a list of categories and connecting it

erikb wrote:

would it be better to make "categories" a separate table?  Then have something like id, category_name, and product_id (foreign key).  Then in the model do the has_many/belongs_to for the two.

Yes. That's how you should do it because it will make adding/removing/changing categories easier throughout your app's lifecycle (i.e. build an interface for it so people don't bug you every time they want to add a category).

vinnie - rails forum admin

Re: Making a list of categories and connecting it

How do I go about making a new product form that has the category field available in it?  I imagine I need two partial forms but how would I call them?  would they be called from "new" in the "/view/admin" or do I make a "new" file in "/view/category". 

Sorry for the basic questions.  I got the VTC.com Ruby on Rails video tutorials which did help me connect some of the dots but it doesn't go too deeply.  Still reading through the Agile Book. 
Thanks again.

Re: Making a list of categories and connecting it

I would create a separate controller for adding/editing/removing categories. It will be much easier than trying to put this functionality in the product form.

Railscasts - Free Ruby on Rails Screencasts

Re: Making a list of categories and connecting it

I started doing that actually, but that may beg the question.....at least for me.....so how do I connect the two, or I guess it would be, where do I put a user connection?

Thanks again.....this is actually helping a lot.

Re: Making a list of categories and connecting it

I'm not sure I understand your question. It all kind of depends how you want the interface to behave.

I just reread the thread, and Danger's suggestion of listing the products in the Category show page is pretty good. Then you could create an "add product to this category" link which will go to the new action in the Products controller (passing the category's id).

Railscasts - Free Ruby on Rails Screencasts

Re: Making a list of categories and connecting it

thank you ryanb.  Your post coupled with rereading danger's post for the 50th time forced an epiphany (i think) of how to put it together.