2013年1月27日 星期日

英文俚語一則

 dafaq  
 【義】Shortened form of "The Fuck". Also used instead of "What The Fuck".   
 【例】Dafaq is this?  


2013年1月23日 星期三

英文俚語三則


 whipped  
 【義】Being completely controlled by your girlfriend or boyfriend  
 【例】He is whipped  

Whipped 通常用在男生身上,意思類似妻(女友)管嚴。



 tramp  
 【義】Any woman who will open her legs for anyone, except for me.  
 【例】She is such a tramp  

中文意思有點像把不到的花蝴蝶(貶意)

























 fucked in the head  
 【義】Idiotic, stupid, not too clever.  
 【例】He is fucked in the head  

就是stupid的意思,簡單稱為腦殘。 下面圖裡的學生就是fucked in the head的經典。






2013年1月20日 星期日

Active Record關係

在【Ruby on Rails Up and Running】一書中,有一整個章節在討論Active Record關係。

說穿了,所謂的Active Record關係(以下簡稱ARR)是用來描述不同model間的關係,或者可以想

成database中不同table間的關係,ARR可用Domain Specific Language(DSL)來描述。


以下方這張簡單的架構圖來說 (ps. 圖片來源參考至【Ruby on Rails Up and Running】chapter 3,

如有侵權,請不嗇告知)




這是一張線上瀏覽圖片的系統之架構,其中每個藍色方塊代表一個model,可以清楚的看到不

同的model間存在一些關係,其中包括 Many to one, 以及Many to many。

所代表的意義類似:

1. 分類(category)和照片(photo)存在多對多關係, 一種分類中會有多張照片,一張照片亦可能

屬於多個分類

2. 投影片(slide)和照片(photo)存在多對一關係,多張投影片可能參照到同一張照片

--

清楚釐清不同model間的關係之後,我們就可以用DSL來描述這些關係。

我們將DSL寫在相關的model中,舉例來說,要描述photo和slide間的關係,

我們可能會把photo寫成下面的樣子:

 class Photo < ActiveRecord::Base  
  has_many :slides  
 end  

另一方面,我們也會把slide寫成:

 class Slide < ActiveRecord::Base  
  belongs_to :photo  
 end  


由上面兩段code,我們清楚的看到兩個重點:

1. photo has many slides

2. slide belongs to (a) photo

藉由這種和自然語言很相像的DSL,我們就可以為每種關係塑模。

除此之外,針對photo和category之間的多對多關係,

我們也可以更改photo的程式碼為:

 class Photo < ActiveRecord::Base  
  has_and_belongs_to_many :categories  
  has_many :slides  
 end  

並且在category model中寫下:

 class Category < ActiveRecord::Base  
  has_and_belongs_to_many :photos  
 end  

一樣的,我們可以很白話的看出當中的關係:

1. Photo has and belongs to many categories

2. Category (also) has and belongs to many photos

如此一來,我們便可對不同model間的關係塑模了。

How to start RoR project

1. start a new project

        rails new project_name

2. create scaffolds for your project, and notice your schema of the model

        rails generate scaffold scaffolding_name schema_name:schema_type

    ps1. if model_name is Photo, then table_name would be photos, controller_name would be PhotosController, and scaffolding_name would be photo. This is what we call "convention"!

    ps2. after we use scaffolds, we will get MVC, but the database is not generated yet until we use migrate command

    ps3. there is no int in schema_type, use integer instead

3. create database

       rails db:migrate

給RoR初學者的小建議

Hi all,

我的練習過程是先找文件,邊讀邊寫。

有個小地方要提醒大家,

RoR是一個迅速發展的語言,

或者更精確地來說,它僅是一種framework,

語言本質上還是Ruby。

離題了,重點是由於迅速的發展,

會出現很多版本上的差異。

以我為例,我手邊有本O'reilly的【Ruby on Rails Up and Running】

裡面就有很多地方和目前的版本不同

包含scaffolding和其他API,若單純照書上所寫的來實作,

會遭遇很多大大小小的問題,

因此,我推薦另一本書給大家【Rails 3 in Action】

或是根據官方文件來練習。

將可免去走冤枉路的風險。


Best,
Brady