Chinaunix首页 | 论坛 | 博客
  • 博客访问: 697442
  • 博文数量: 160
  • 博客积分: 8847
  • 博客等级: 中将
  • 技术积分: 1656
  • 用 户 组: 普通用户
  • 注册时间: 2010-11-25 16:46
个人简介

。。。。。。。。。。。。。。。。。。。。。。

文章分类

全部博文(160)

文章存档

2015年(1)

2013年(1)

2012年(4)

2011年(26)

2010年(14)

2009年(36)

2008年(38)

2007年(39)

2006年(1)

分类:

2010-11-26 13:35:44

1:products/orders/line_items表的关系:
mysql -uroot -plivedoor2008 depot_development -e "show create table products"

CREATE TABLE `orders` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `address` text COLLATE utf8_unicode_ci,
  `email` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `pay_type` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `products` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `title` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `description` text COLLATE utf8_unicode_ci,
  `image_url` varchar(255) COLLATE utf8_unicode_ci DEFAULT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  `price` decimal(8,2) DEFAULT '0.00',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;

CREATE TABLE `line_items` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `product_id` int(11) NOT NULL,
  `order_id` int(11) NOT NULL,
  `quantity` int(11) NOT NULL,
  `total_price` decimal(8,2) NOT NULL,
  `created_at` datetime DEFAULT NULL,
  `updated_at` datetime DEFAULT NULL,
  PRIMARY KEY (`id`),
  FOREIGN KEY (`fk_product_id`) REFERENCES `products` (`id`),
  FOREIGN KEY (`fk_order_id`) REFERENCES `orders` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;


一个订单(order)中可以包含多个订单项(line_item),
一个产品(product)可能包含在多个订单项(line_item)中.

所以,order.rb模型/product.rb模型和line_item.rb模型的关系就是:
order.rb
has_many :line_items;

line_item.rb
belongs_to :order;
belongs_to :product;

正是因为有了这样的关系,所以,在控制器/模型或者视图中,
li = LineItem.find_by_id(:all, :condition => ....);
li.order.order_name;
...

or = Order.find(...)
or.line_items.size


belongs_to和has_many这2个方法,增加了模型互相之间可以调用对方。

但是rails的数据表的建立是基于数据迁移的,所以我们不直接使用DDL语句,而是:
ruby script/generate migration create_line_item


class CreateLineItems < ActiveRecord::Migration
    def self.up
        create_table :line_items do |t|
            t.integer :product_id, :null => false, :options => "CONSTRAINT fk_line_item_products REFERENCES products(id)"
            t.integer :order_id, :null => false, :options => "CONSTRAINT fk_line_item_orders REFERENCES orders(id)"
            t.integer :quantity, :null => false
            t.decimal :total_price, :null => false, :precision => 8, :scale => 2
            t.timestamps
        end
    end

    def self.down
        drop_table :line_items
    end
end


其中的:
:options => ...
表示了表之间的外键关联关系,
当我们执行:
rake db:migrate
的以后,
mysql -uroot -plivedoor2008 depot_development -e "show create table products"
外键设置并没有起作用。

所以,为了保险起见,将当前的数据库迁移版本回溯到上一个版本,采用DDL的方式比较好。
阅读(1105) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~