Topic: has_many :through question

Hi all,

I am a new student of Rails, which has been great so far. I'm now practicing database connection with Rails, and have a rather simple question regarding has_many :through. I've looked at some past posts, but they only cover the model-design, not the accompanying controller actions.

Basically, I want to create the simplest possible shopping cart.

Back-end outline:
xitems has two columns: id, name
xlists has two columns: id, title
I'm bridging the two via xtracks which has three columns: xlist_id, xitem_id, xcount (all ints)

Front-end outline:
A view for xlists with a text-field for title, a drop-down containing the names of all xitems, and a text-field for specifying the count (how many of the selected xitem the user wants).

Create a scaffold for xlist.
Then, apply "has_many :through", as follows:

class Xitem < ActiveRecord::Base
has_many :xtracks
has_many :xlists, :through => :xtracks

class Xlist < ActiveRecord::Base
has_many :xtracks
has_many :xitems, :through => :xtracks

class Xtrack < ActiveRecord::Base
belongs_to :xlist
belongs_to :xitem

Question 1:
Since I've been using the scaffold generator until now for any kind of CRUD, I'm not sure how to implement CRUD manually via has_many :through.

Since I've put the default scaffold for xlist, when I create a new entry, it only allows me to edit xlist's columns, not xitem's. So what do I have to add to the xlist controller to relate the three tables? That is, how do I go about storing the data in a linked manner?

Thank you.


Last edited by tabletennis001 (2007-02-09 22:36:38)

Re: has_many :through question

See my tutorials on creating and editing multiple models in a single form. Although you do have three models here, you are only editing two at a time (an xlist and its xtracks). You don't even need to worry about xitem - you just use a collection_select dropdown menu for setting the xitem_id attribute in xtrack and that's it. Don't even worry about has_many :through. You are just dealing with the simple has_many/belongs_to association between xlist and xtrack.

If you need some code specific to your project I can probably post some.

Railscasts - Free Ruby on Rails Screencasts

Re: has_many :through question

I'll go through your tutorials and let you know what happens.
Thank you.

Re: has_many :through question

The tutorials are splendid (I even tested out the variable-number-of-items functionality with RJS!). All of my relevant db-access problems were solved by them. Except the problem of displaying the items...

One can display the xlists, which has_many :xitems, simply by @mylists = Xlist.find_all, but once the user selects a specific xlist, how do I display the xitems that belong to THAT xlist? I suppose I could write @myitems = Xitem.find(:all, :conditions => "xlist_id = '"...), but is there a simpler way that Rails allows, given that we've set up the has_many property?


Last edited by tabletennis001 (2007-02-12 00:20:46)

Re: has_many :through question

I've solved my problem. For everyone else's reference:

To retrieve items from xitems table, linked to lists from xlists table, where xlist has_many :xitems

Get all lists including the linked items:
@xlist = xlist.find(:all, :include => 'xitems')

Then, e.g., print them out (or use a for-loop to print all):

to print id of first xlist from the fetched array

to print id of fifth xitem linked to third xlist from the fetched array


Last edited by tabletennis001 (2007-02-12 00:35:41)