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