Day-27 : Model 一对多

续上一篇,今天要来讲的是一对多
还记得吗?我们昨天说的
我们希望每一间商店可以贩售许多种商品,这就是我们所谓的一对多!


  1. 建立 Product Model
    https://ithelp.ithome.com.tw/upload/images/20211011/20140259XkaDzMKJKo.png

2.执行 rails db:migrate
https://ithelp.ithome.com.tw/upload/images/20211011/20140259r8OR81gyUe.png

  1. Store Model 加上has_many将设定成为(一对多)
    https://ithelp.ithome.com.tw/upload/images/20211011/20140259d0QgJEIgXJ.png

4.反之,在Product Model 加上belongs_to
https://ithelp.ithome.com.tw/upload/images/20211011/201402590d9NzzNYlV.png

以上,这些动作,他大概的关联图如下表:
https://ithelp.ithome.com.tw/upload/images/20211011/20140259A3NfvSnnb6.png


老样子,我们进 rails c 看看

  1. 找出一间商店
 > store1 = Store.first
   (0.5ms)  SELECT sqlite_version(*)
  Store Load (0.1ms)  SELECT "stores".* FROM "stores" ORDER BY "stores"."id" ASC LIMIT ?  [["LIMIT", 1]]
 => #<Store id: 1, title: "良心商店", tel: nil, address: nil, user_id: 1, created_at: "2021-10-10 09:...

2.建立两件商品的资料,分别是product1和product2

> product1 = Product.new(name:"吃吃喝喝商店",price: 200)
 => #<Product id: nil, name: "吃吃喝喝商店", description: nil, price: 0.2e3, is_available: nil, store...
 > product2 = Product.new(name:"我想要飞",price: 1000)
 => #<Product id: nil, name: "我想要飞", description: nil, price: 0.1e4, is_available: nil, store_id:...

3.把这两个商品用阵列的方式,丢进去给store1

 > store1.products = [product1, product2]
  Product Load (0.4ms)  SELECT "products".* FROM "products" WHERE "products"."store_id" = ?  [["store_id", 1]]
  TRANSACTION (0.1ms)  begin transaction
  Product Create (0.4ms)  INSERT INTO "products" ("name", "price", "store_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["name", "吃吃喝喝商店"], ["price", 200.0], ["store_id", 1], ["created_at", "2021-10-11 08:29:21.580320"], ["updated_at", "2021-10-11 08:29:21.580320"]]
  Product Create (0.1ms)  INSERT INTO "products" ("name", "price", "store_id", "created_at", "updated_at") VALUES (?, ?, ?, ?, ?)  [["name", "我想要飞"], ["price", 1000.0], ["store_id", 1], ["created_at", "2021-10-11 08:29:21.581872"], ["updated_at", "2021-10-11 08:29:21.581872"]]
  TRANSACTION (0.5ms)  commit transaction
 => [#<Product id: 1, name: "吃吃喝喝商店", description: nil, price: 0.2e3, is_available: nil, store_...

4.接下来,让我们来看看,资料是不是真的有写进去

> store1.products
 => #<ActiveRecord::Associations::CollectionProxy [
 #<Product id: 1, name: "吃吃喝喝商店", description: nil, price: 0.2e3, is_available: nil, store_id: 1, created_at: "2021-10-11 08:29:21.580320000 +0000", updated_at: "2021-10-11 08:29:21.580320000 +0000">, 
 #<Product id: 2, name: "我想要飞", description: nil, price: 0.1e4, is_available: nil, store_id: 1, created_at: "2021-10-11 08:29:21.581872000 +0000", updated_at: "2021-10-11 08:29:21.581872000 +0000">
 ]>

5.确认这些资料有几笔?

> store1.products.count
   (0.5ms)  SELECT COUNT(*) FROM "products" WHERE "products"."store_id" = ?  [["store_id", 1]]
 => 2

今天就先到这边,明天我们来讲多对多!
最近刚好有在练习这区块,就把一些书上的重点来写一篇。
看看自己在步骤上面是否有记住
希望飞得慢,可以成功抵达终点拉~

/images/emoticon/emoticon14.gif

参考资料:为自己学Ruby on Rails


<<:  [Day 29] 使用 Python Flask 架设 API 吧!

>>:  Day 27 记忆体的管理

ASP.NET MVC 从入门到放弃 (Day5) -C# 判断式 回圈介绍

接着来讲讲常用的判断式写法.... 简单来说以下就是玩攻略游戏 在选择选项的逻辑.... 单项if写...

D-19 网页站台 ? webapp ? mvc ? webapi

网页站台初体验 在昨日最後大头跟小光说c#的介绍告一段落了,所以今天开始会进入网页站台相关知识的介绍...

Day 29 - Vanilla JS Countdown Timer

前言 JS 30 是由加拿大的全端工程师 Wes Bos 免费提供的 JavaScript 简单应用...

Day 12 均衡思考赚钱与不被花钱

依现况而言企业在强大的个资隐私规范之下,如何符合规范又不被在不知情况之下被罚又不知所云,在个资自主当...

Service Container

Service Container 是 Laravel 框架中相当重点的一个功能,主要是用来节省撰写...