Ruby on Rails Development ab initio (53)-ActiveRecord Foundation (Table Association)

Source: Internet
Author: User
Tags one table ruby on rails

Many programs use a database that contains more than one table, and often there are relationships between tables, orders often contain multiple entries, and an item is associated with a commodity, a commodity may belong to multiple commodity categories, and a commodity classification contains a number of different goods.

In a database, these associations are represented by using a primary key value to associate a table with a foreign key, but this is the underlying category where we need to handle the association between the model objects rather than the columns and keys in the database. If an order contains more than one entry, we need to have a way to maintain, deal with their relationship, and if an item is referenced to a product, we might want to do this:

Price = Line_item.product.price

Rather than trouble like the following:

product_id = line_item.product_id
Product = Product.find (product_id) Price
= Product.price

Active record can help us, as part of an ORM, the active record converts a foreign key from a lower-level database into a high-level mapping between objects. Three basic situations were dealt with:

A record in table A is associated with 0 or one record in table B.

A record in table A and any number of records in table B are associated.

Any number of records in table A and any number of records in table B are associated.

Let's look at how to create a foreign key (Foreign key), we use the following DDL to create the table, which specifies an association between:

CREATE TABLE products (
ID int is not NULL auto_increment,
title varchar is not NULL,/
* ... * *
primary k EY (ID)
);
CREATE TABLE orders (
ID int not NULL auto_increment,
name varchar (m) not NULL,/
* ... */
PRIMARY KEY ( ID)
);
CREATE TABLE Line_items (
ID int not NULL auto_increment,
product_id int not NULL,
order_id Int. not null,
  quantity int NOT NULL default 0,
unit_price float (10,2) NOT NULL,
constraint fk_items_product foreign key (p RODUCT_ID) references products (ID),
constraint Fk_items_order foreign key (order_id) references orders (ID),
Primary key (ID)
);

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.