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 D. 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!

-Scott

Scott Parks wrote:

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?

Hi Scott,

I’m no expert and fairly inexperienced in Ruby and Rails.

However, based on treating things as objects, it sounds like products
have four subclasses. I would think you really want one Product class
and then four subclasses in the model, type1…4. This would allow for a
lot of code to be reused for common product attributes.

I’m not sure you will be able to easily (or in a standard way) re-use
the controller for products and carts. It seems like there would be a
lot of code re-use for each product if in a different table. There
might be some advanced polymorphic product table way of doing this, but
it seems like adding a fifth table is going in the wrong direction.

Bill

On Dec 3, 2006, at 3:32 PM, devaulw wrote:

but
it seems like adding a fifth table is going in the wrong direction.

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.

-Scott

Scott Parks wrote:

On Dec 3, 2006, at 3:32 PM, devaulw wrote:

but
it seems like adding a fifth table is going in the wrong direction.

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.

The potential for lack of DRYness makes me think you’re on the wrong
path. I see that some instances of product will have unneeded
attributes if you roll them up. Try to see which attributes are shared
and put those into a Product class. Then subclasses inherit from
Product to have these other attributes. It may also be possible to put
the other attributes in a field that is itself a YAML. I’m not sure
what validation you need to do on the attributes, but the YAML attribute
is another possibility.

Perhaps someone with a little more Ruby/Rails chops than I have can
offer some guidance.

Bill

On Dec 3, 2006, at 3:52 PM, devaulw wrote:

is another possibility.

Perhaps someone with a little more Ruby/Rails chops than I have can
offer some guidance.

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
id
type

Fabric Table
id
fk_item_id
title
description
make
type
content, etc
photo1
photo2

Product Table
id
fk_item_id
title
name
size
content
photo1

Magazine Table
id
fk_item_id
name
issuedate
issueyear
language, etc

Pattern Table
id
fk_item_id
title
maker
photo1
photo2
photo3

I shortened the tables, but you get the idea.

Thank you in advance.
-Scott

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
item.id.

On Dec 3, 2006, at 3:52 PM, devaulw wrote:

is another possibility.

Perhaps someone with a little more Ruby/Rails chops than I have can
offer some guidance.

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

Thanks again.

-Scott

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.

This is what I had been thinking. Also, photos and makers could be
moved to a different table. A Product has_many photos and makers is
what I’m thinking. This way you can easily grab things by maker and/or
sort/review photos for a gallery.

Consider that each Product has a name (not a title) and perhaps a
description, maker and a price. Then specific attributes are defined
within subclasses of the Product or in a separate table of attributes.
I think you would need to define the rules for the additional properties
in the product model.

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])

<%=product.details.photo1%>

Fredrik

“”“Taylor S. ÐÉÓÁÌ(Á):
“””

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
item.id.


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

Hello!

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.

On Dec 4, 2006, at 12:54 AM, Taylor S. wrote:

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
item.id.

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
find(:all,
:conditions => “producttype = ‘Product’”,
#:conditions => “dateactive <= now()”,
:order => “title desc”)
end

def self.magazine_items
find(:all,
:conditions => “producttype = ‘Magazine’”,
#:conditions => “dateactive <= now()”,
:order => “title desc”)
end

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!

-Scott

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

Look here:
http://twelvelabs.com/singletable/index.html
http://www.juixe.com/techknow/index.php/2006/06/03/rails-single-table-inheritance/

Jeremy

Scott Parks 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
find(:all,
:conditions => “producttype = ‘Product’”,
#:conditions => “dateactive <= now()”,
:order => “title desc”)
end

def self.magazine_items
find(:all,
:conditions => “producttype = ‘Magazine’”,
#:conditions => “dateactive <= now()”,
:order => “title desc”)
end

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.

Is there some way to scope based on the different types of product the
different attributes and validations you’ll need to make this work?
This seems like the work of the model. I see something on page 372 of
AWDWR version 2 that describes validation if scenarios. Perhaps that
will help you with the validation. Then you’ve got to deal with
attributes that you don’t want for certain types of products so those
fields do not appear.

Is there a way to get ActiveRecord to ignore certain fields in a
subclass? Perhaps there is something in David Black’s book on using a
subclass to overwrite/ignore a parent attribute.

Jeremy McAnally wrote:

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

Look here:
http://twelvelabs.com/singletable/index.html
http://www.juixe.com/techknow/index.php/2006/06/03/rails-single-table-inheritance/

Jeremy

That sounds like the solution (I have a problem similar to Scott’s).
Thanks for the links.

On Dec 4, 2006, at 5:31 PM, devaulw wrote:

Thanks for the links.
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!

-Scott