Ruby on Rails

Ruby on Rails long string id

之前有講到使用 UUID 當作 primary key 的方法,因為 postgresql 有提供 uuid,可以直接拿來使用。

接下來的這個方法適用於所有的資料庫,在物件儲存之前先給定它的 id,如此一來就能定義我們所想要的內容了。

先隨便用鷹架產生點東西:

1
rails g scaffold book title

加上 id: :string 讓 id 的型態為 string。

1
2
3
4
5
6
7
8
9
# db/migrate/20170218105724_create_books.rb
class CreateBooks < ActiveRecord::Migration[5.0]
def change
create_table :books, id: :string do |t|
t.string :title
t.timestamps
end
end
end

閱讀全文

Ruby on Rails 5 dynamic subdomains

一個使用者有名字 name 還有自己網頁的子網域 subdomain,我們想要的結果是讓每一位使用者有自己的子網域,而且還可以隨時更改。

假設現在有個使用者是 User.create(name: "akii", subdomain: "akiicat"),在 rails 預設 show 頁面會是 http://localhost:3000/users/1,但我們想把網址變成個人的子網域 http://akiicat.localhost:3000

接下來會用鷹架建立使用者來做示範:

1
2
rails g scaffold user name subdomain
rake db:migrate

修改 routes.rb,加上首頁,然後把 show 的頁面加上 subdomains 的限制。

1
2
3
4
5
6
7
8
# config/routes.rb
Rails.application.routes.draw do
resources :users
constraints subdomains: /.+/ do
get '', to: 'users#show'
end
root to: 'users#index'
end

閱讀全文

Ruby on Rails 5 api subdomain

為了要 demo api subdomain 的頁面是否能夠運作,這邊用鷹架隨便建立個東西。

1
2
rails g scaffold api::v1::books title
rake db:migrate

使用 constraints 限制 subdomain: 'api'

然後用 scope module: 'api' 加上 app/controllers/api 這個資料夾。

1
2
3
4
5
6
7
8
9
10
# config/routes.rb
Rails.application.routes.draw do
constraints subdomain: 'api' do
scope module: 'api' do
namespace :v1 do
resources :books
end
end
end
end

閱讀全文

Ruby on Rails callback 筆記

可用的 callback

以下是 Active Record 可用的 callback,依照執行順序排序:

新建物件

1
2
3
4
5
6
7
8
9
before_validation
after_validation
before_save
around_save
before_create
around_create
after_create
after_save
after_commit/after_rollback

更新物件

1
2
3
4
5
6
7
8
9
before_validation
after_validation
before_save
around_save
before_update
around_update
after_update
after_save
after_commit/after_rollback

閱讀全文

Ruby on Rails foreign key must exist 的問題

rails 在這次的更動之後,如果 belongs_to 的 foreign key 不存在的時,會產生驗證錯誤。

1
2
3
4
5
6
7
8
9
class Author < ActiveRecord::Base
end

class Book < ActiveRecord::Base
belongs_to :author
end

book = Book.create(title: "book")
book.errors.full_messages # => ["Author must exist"]

上面的例子可以看到,我們沒辦法建立 Book 的紀錄。

options

  • required: 如果設為 true 的話,會驗證關聯是否存在。在 rails 5 預設為 true
  • optional: 如果設為 true 的話,不會驗證關聯是否存在。

上面兩個選項都是驗證關聯本身,不是驗證 foreign key 的 id。

閱讀全文

Ruby on Rails 探索 inverse_of

inverse_of

主要功能會去通知對方自己的狀態,可以解決物件不同步的問題。

在 belongs_to 裡的 :inverse_of 會去找尋所對應的 has_one has_many 上相同的名稱,來通知他們之間的關係。

在 has_one has_many 裡的 :inverse_of 會去找尋所對應的 belongs_to 上相同的名稱,來通知他們之間的關係。

如果沒有寫 :inverse_of 這參數,rails 會使用 heuristic algorithm 去猜測名稱,但在如果不是使用標準名稱的話會失效。

Without inverse_of

沒有 :inverse_of 會發生物件不同步的問題,可以看以下的範例:

1
2
3
4
5
6
7
class Author < ActiveRecord::Base
has_many :books
end

class Book < ActiveRecord::Base
belongs_to :author, inverse_of: false
end

閱讀全文

Ruby on Rails Multiple Foreign Keys

這篇主要是在講說,在一個 Table 裡面有多個 Foreign Keys 會指向同一個 table。例子如下,一首歌 Song 會有歌手 (singer_id) 跟作曲者 (composer_id),都會指向同一個 Singer 的 table。

1
2
3
4
5
6
7
8
┌──────────────────┐                ┌─────────────────────┐
│ Singer │ │ Song │
├──────────────────┤ ├─────────────────────┤
│ id:integer │←───────┐ │ id:integer │
│ name:string │ ├───────│ singer_id:integer │
│ │ └───────│ composer_id:integer │
│ │ │ title:string │
└──────────────────┘ └─────────────────────┘

閱讀全文

Ruby on Rails Eager Loading 加速:一次拿取所以資料

這個在 rails 裡面,資料有關聯的時候,會產生的一些效能上的問題,假設我們的例子如下:

1
2
3
4
5
6
7
┌──────────────────┐                ┌───────────────────┐
│ Author │ │ Book │
├──────────────────┤ ├───────────────────┤
│ id:integer │←───────┐ │ id:integer │
│ name:string │ └───────│ author_id:integer │
│ │ │ title:string │
└──────────────────┘ └───────────────────┘

當我們在 books controllers 拿了一群東西,像是有 all 或是 where

1
2
@books = Book.all
@books = Book.where(author: @author)

常常接著又在 view 裡面使用 each 抓取了關聯的東西 author,這時 @books 不知道 author 的內容所以又必須呼叫一次 SQL 指令去拿資料,所以當資料量一大的時候,會產生效能上的問題。

1
2
3
@books.each do |book|
book.author
end

閱讀全文

Ruby on Rails 重新設置 Database

重設 Database 的方法

Status

在重新設置資料庫前先執行 db:migrate:status,看看現在資料庫的 migrate 的狀態是什麼。

1
2
3
4
5
6
7
8
# rake db:migrate:status
database: dev

Status Migration ID Migration Name
--------------------------------------------------
up 20161114101612 Enable uuid extension
up 20161115193547 Create singers
up 20161115193548 Create songs

Basic

在重新設置資料庫時,最簡單的方法就是先把資料庫刪除,然後重新建一個,最後在 migrate:

1
2
db:drop db:create db:migrate
rake db:migrate:reset

閱讀全文

Ruby on Rails Assets Pipeline 的使用方式

Assets Pipeline

config/initializers/assets.rb 中把 assets 加入 precompile 的路徑,不建議使用 *.css *.js 加入所有的東西,因為會將一些有的沒的或是不需要的東西一起編譯進去。

assets pipeline 的主要好處就是把所有的 css 包成一個檔案,漸少 request 的數量,像是 application.css 會載入所有被 require 的檔案,最後只需要傳送一個 css 就行了,同理 javascript 也是如此。

Layout

看不懂在說什麼,直接看例子比較快。

假設現在我們的 layout 裡面有兩個不同的頁面 admin.html.erb 跟 user.html.erb,要分別讓他們使用不同的 css js。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
app
├--assets
│ ├--javascripts
│ │ ├--admin.js
│ │ ├--user.js
│ │ └--...
│ └--stylesheets
│ ├--admin.scss
│ ├--user.scss
│ └--...
└--view
└--layout
├--admin.html.erb
└--user.html.erb

但為了減少請求的數量,我們讓一個 layout 只需要一個 css 和一個 js,在 layout 中的話會像這樣:

1
2
3
4
5
6
7
# app/views/layouts/admin.html.erb
<%= stylesheet_link_tag 'admin', media: 'all' %>
<%= javascript_include_tag 'admin' %>

# app/views/layouts/user.html.erb
<%= stylesheet_link_tag 'user', media: 'all' %>
<%= javascript_include_tag 'user' %>

如果使用 admin 的 layout 就會載入 admin.css admin.js

如果使用 user 的 layout 就會載入 user.css user.js

閱讀全文