Ruby on Rails development from scratch (Windows) (eight)-Create a cart with session

Source: Internet
Author: User
Tags command line ruby on rails

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.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.