2009年3月18日 星期三

The Rails Way :CRUD, Advanced Find

1.TRW p177
User model has many gookus
>> u.gooku_ids
=> [1566, 1284, 2088, 2094, 4016, 4956, 8114]
>> u.gookus.map(&:id)
=> [1566, 1284, 2088, 2094, 4016, 4956, 8114]

所以在命名像是checkbox 就可以取成 user[gooku_ids][]

2.query cache TRWp172

Spend less time in the database, and more time outdoors!

3.Controlleing Access to attributes, TRWp178


4.Database Locking
4.1 Optimistic Locking
implement 很簡單, 加上lock_version 的column 之後給rails 處理就好
而race conditions 輸的人會丟出ActiveRecord::StaleObjectError 的 execption

優點是 implement 很簡單
缺點是有點慢, 而且等到update 做了之後才報錯, 使用者又在要作一次

4.2 Pessimistic Locking
需要database 的支援, 在會做到update 時的動作, 利用lock 也把reading 鎖起來, 避免讀到stale data

Pessimistic locking takes place at the database level. The SELECT statement generated
by ActiveRecord will have a FOR UPDATE (or similar) clause added to it, causing
all other connections to be blocked from access to the rows returned by the select
statement. The lock is released once the transaction is committed. There are theoretically
situations (Rails process goes boom mid-transaction?!) where the lock would not
be released until the connection is terminated or times out.


4.3 Consideration
Web applications scale best with optimistic locking, which as we’ve discussed doesn’t
really use any locking at all. However, you have to add application logic to handle failure
cases

就是用optimistic 就對了

5. select, group option

沒有留言: