Extend Agile Web Dev depot example

Hi, newbie here, but man from what I have seen of Rails I feel like I have been in the dark ages.

I am trying to extend the depot app in the Agile Web Development w/Rails book and have a question
before I dive too far into this project.

I am thinking of using this for a customer project and they will have 4 distinct product types, which
is using 4 different tables and yes, under PHP that was 4 different forms.

I generated my model for the product table, but will also need one for gallery, accessories and patterns.
Can I have all those, coming from 4 different tables and keep it in the rails framework?

I will need to also have a shopping cart that will allow the user to add from any one of those tables to
their shopping and seeing as how Rails uses the ID of the column in the table, how do I direct it to
each table?

Thank you in advance, I have not been this excited about web development in a very long time!


Hi Bill,

Thank you for the response, I have been sitting back and thinking of a solution.
The reason there are 4 different tables was because each product line has different
attributes, for example: accessories will have the option of size, but in the fabric
table that is not needed and instead we would have things like fabric content.

That is why the multiple tables. One possible solution would be to have a master
table that would hold the id, then the tables for each product line that would use
the ID key from the master table.

And just a guess, but I am already going against the entire Rails philosophy of over-
thinking this.


Hi Bill-

I think I am getting confused with the entire model thing, but after sketching it
out on paper of all things I am pretty sure I have a better understanding and
a way to do what I need to do.

belongs_to is my friend!

My trouble was with the keys so I have a master items table with sub tables
containing the different types of products.

I am sure there is a better way, but for now I will concentrate on learning then

Thanks again.


I just looked at the db structure with the sites current environment, I am open to
DRY suggestions on how to make this simpler in Rails development.

Items Table

Fabric Table
content, etc

Product Table

Magazine Table
language, etc

Pattern Table

I shortened the tables, but you get the idea.

Thank you in advance.

"""Taylor Strait писал(а):

Its not really clear why these are subclasses. They have no common
attributes, only an arbitrary ID number. So why not make each table
its own Model and drop the pointless Items table? On the other hand, if
Items.id is not an autoincrement number and a real product identifier, a
reason to keep the Items table would be to enforce the uniqueness of

Posted via http://www.ruby-forum.com/.


I would like to recommend you the following way:

1. Create AbstractObject in database and in model. It'll has name, id,
price and all attributes, that every object have.
2. Create Property table and model and then give AbstractObject
has_many :properties

3. Then if you want to be more object oriented - you can create the
classes for all object types and fill their default properties.

+ of this way: You can create only one form, cart and other views for
every product.

From looking briefly at your structure, you could probably move the

name to the products class (one is named title).
Beyond that, perhaps you could take a look at polymorphism. It will
allow you to find a product and automatically have the subclass
assigned to a class variable. (I'm using details for this example) ex.

product = Product.find(params[:id])



devaulw wrote:

What I ended up doing (for the sake of speed) was creating a single table
that holds everything, some of the items will use all columns, the others
will not.

I then created the following in the product model:

   def self.salable_items
     :conditions => "producttype = 'Product'",
     #:conditions => "dateactive <= now()",
     :order => "title desc")

   def self.magazine_items
     :conditions => "producttype = 'Magazine'",
     #:conditions => "dateactive <= now()",
     :order => "title desc")

This gives me the views on the customer side of the site that I want.

But, on the admin side of the site I will need to create 4 different forms that
will only display the fields needed for the particular product.

For example - if this is fabric, I want to display only the fields that are relevant
for fabric, same with magazines, etc.

Sorry to be so lost (and I have a habit of over thinking things), but how do I go
about that.

Thank you!


Why not just use Single Table Inheritance? Or am I misunderstanding
what you need?

Look here:


I second that thanks for the links, although I can't get to the second site right now, but
I am going by memory from what I read today.

I *believe* I have solved the problem by doing this:

I created a new model called magazine:

class Magazine < Product

Product is my master form that shows all the fields.

Created magazine.rhtml in app/views/admin which remders _magazine.rhtml

So far, so good.

A different way of thinking for us that have been doing it differently in php, classic asp,
etc. But I LIKE it!