sti, mti in ruby on rails

32
Gamadian with Carrierwave Polymorphic Association & Singe Table Inheritance 2016/09/09 Wei-Yi, Chiu

Upload: wei-yi-chiu

Post on 13-Apr-2017

133 views

Category:

Software


2 download

TRANSCRIPT

Gamadian with Carrierwave

Polymorphic Association & Singe Table Inheritance

2016/09/09 Wei-Yi, Chiu

Outline

Introduce of / How to Carrierwave

What is Polymorphic Association(MTI)

How to Polymorphic Association

What is Single Table Inheritance(STI)

How to STI

「Classier solution for file uploads for Rails 」

–Carrierwave team

選用原因

已經發展超過4年的上傳檔案套件

有完整的add-ons與doc可以隨時加強上傳檔案的功能

e.g) backgrounder, viewer, video-thumbnailer

原理

How to Carrierwave

User story - 1

小麵要可以從後台上傳賣場圖片到s3

圖片上傳前要先最佳化

圖片要自動分成六種尺寸

Tutorial

gemfile:

command:

Tutorial

gamaitem.rb :

form.html.erb :

image_uploader.rb :

Tutorialconfig/initializers/carrierwave.rb

Tutorial

controller

什麼都不用動啊

What is polymorphic Associations

intro單個關聯裡,model可以屬於多個model

User story - 2

Ethan要從後台傳cover, profile, loadings到s3

一樣要最佳化跟切圖

爲屹希望可以用一張表(assets)管理所有圖片

「那就先改gamaitem的架構吧」

–爲屹

Tutorial

command:

Tutorialmodel :

Tutorialform.html.erb :

Tutorial

controller 還是

什麼都不用動啊

Rails Skill

Nested form

Polymorphic Associations

Something like STI ???

What is STI

intro到貨通知有SMS、Email、推播

How to STI

User story - 1

dean希望server可以自動通知客人到貨狀態

要可以從後台檢視、補發通知

通知的方式有:簡訊、e-mail、推播

Tutorialcommand :

migration :

Tutorialmodels/order_notification.rb

Tutorialmodels/order_notification/short_message_service.rb

Tutorialmodels/order_notification/smart_phone_noti.rb

Tutorial

application.rb

models/order_notification/email_noti.rb

Tutorial

controller :

這就是資料表欄位不能取名type的原因

使用時機

Polymorphic當我們擁有多個model共用同一個model時無法使用主鍵(id)有效地select出資料時

STI當我們擁有多個一樣特性的model時使用 主鍵(id)可以有效地select出資料時

Next

haml: 寫Html超級快,可是維護有點難

meta-programming: 用程式寫程式