In the previous section, we demonstrated how to build a list of items, and this time we built a simple shopping cart based on the previous content.
1. First we want to create a table that holds customer shopping information:
Database scripts:
drop table if exists line_items;
CREATE TABLE Line_items (
ID int not NULL auto_increment,
product_id int not null,
quantity int NOT null DEFAU Lt 0,
unit_price decimal (10,2) NOT NULL,
constraint fk_items_product foreign key (product_id) references Products (ID),
primary key (ID)
);
Then create the table in phpMyAdmin and then use the Rails command line to create the corresponding class for the Line_item table:
depot> Ruby Script/generate Model LineItem
(The detailed steps you create can refer to a few of the previous essays)
2. Create a master-slave relationship for LineItem and product:
Open the Line_item.rb file in the \rails_apps\depot\app\models directory and modify the contents of the file as follows:
Class LineItem < activerecord::base
belongs_to:p roduct
End
You can see belongs_to:p roduct This sentence creates a master-slave relationship for LineItem and product.