Ruby on rails开发从头来(windows)(八)-使用Session创建购物车2011-12-02 博客园 Cure在前面的内容里,我们演示了怎样构建一个商品的列表,这次,我们在前面内容的基础上,构建一个简单的购物车。1.首先我们要来创建一个保存客户购物信息的表:数据库脚本:
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 default 0,unit_price decimal(10,2) not null,constraint fk_items_product foreign key (product_id) references products(id),primary key (id));
之后在PhpMyAdmin中创建表,然后使用Rails命令行创建line_item表对应的类:depot> ruby script/generate model LineItem(创建的详细步骤可以参考前面的几篇随笔)2.给LineItem和Product创建主从关系:打开
ails_appsdepotappmodels目录下的line_item.rb文件,修改文件内容为:
class LineItem < ActiveRecord::Basebelongs_to :productend
可以看到belongs_to :product这句给LineItem和Product创建了主从关系。