Archive for May, 2007

Paginator in Rails

Friday, May 25th, 2007

paginator 绝对是任何web app必备之物,rails 1.1.6 内建的paginator很好用,很傻瓜,但是貌似它的处理方式也确实很傻,有很多感觉不爽的地方。有一个find plugin可以完成paginator的活,但是在我的系统上没跑起来,也没研究直接换Bruce的paginator gem,用起来还算顺手。View上面的表现需要自己完成,找到了一个方法,修改一下与paginator gem整合起来,跑的不错。

windowed_pagination_links这个helper让我改成了下面的样子:

  1. def windowed_pagination_links(pagingEnum, pager, options)
  2.     link_to_current_page = options[:link_to_current_page]
  3.     always_show_anchors = options[:always_show_anchors]
  4.     padding = options[:window_size]
  5.  
  6.     current_page = pagingEnum.number
  7.         html = ''
  8.  
  9.     #Calculate the window start and end pages
  10.     padding = padding < 0 ? 0 : padding
  11.         first = current_page-padding<1 ? 1 : current_page-padding
  12.       last    = current_page+padding>pager.number_of_pages ? pager.number_of_pages : current_page+padding   
  13.  
  14.     # Print start page if anchors are enabled
  15.     html << yield(1) if always_show_anchors and not first == 1
  16.  
  17.     # Print window pages
  18.     first.upto(last) do |page|
  19.       (current_page == page && !link_to_current_page) ? html << page : html << yield(page)
  20.     end
  21.  
  22.     # Print end page if anchors are enabled
  23.     html << yield(pager.number_of_pages) if always_show_anchors and not last == pager.number_of_pages
  24.     html
  25.   end

iTerm & SMTH

Friday, May 25th, 2007

可以用iTerm上SMTH了,其实就是telnet方式:telnet -8 bbs.newsmth.net ,存一个书签然后就方便了,还要选一下profile,语言我选的是windows兼容的简体中文,配色方案一换,字体选好,基本就OK了。具体的配置在smth apple版里有一个关于iTerm的精华贴。

term上bbs速度就是快,不过iTerm感觉很慢,莫非是render太慢了?无论我选什么字号出来都是那么点,只有cmd + 几次,然后感觉就慢了。同样的网络下用FTerm就很快。

常用的键:

= 同主题第一篇
p 进文章后进行同主题阅读
$ 文章列表模式下转到最新文章
K 标记已读
F 将文章内容寄到自己的信箱里
f 整体标记文章为已读
x 进入精华版
r 读文章
j 下一篇
k 上一篇
ctrl + p 发文章
ctrl + w 确定发送
r 回复文章

还有一陀一陀的,慢慢来。感谢KDr2

use Twitter in QuickSilver

Thursday, May 17th, 2007

Ooops, I can send message to Twitter from QuickSilver.

mac is wonderful …

NowaZhu

Tweet = Twitter + Quicksilver

MySQL encoding and converting databases to UTF8

Wednesday, May 16th, 2007

1.) Dump the DB:
mysqldump –user=username –password=password –default-character-set=latin1 –skip-set-charset dbname > dump.sql

2.) Replace all latin1 instances with utf8:
sed -r ’s/latin1/utf8/g’ dump.sql > dump_utf.sql

3.) Delete the old DB, create a new one in UTF8:
mysql –user=username –password=password –execute=”DROP DATABASE dbname; CREATE DATABASE dbname CHARACTER SET utf8 COLLATE utf8_general_ci;”

4.) Load the dump into the new DB:
mysql –user=username –password=password –default-character-set=utf8 dbname < dump_utf.sql

original link 

ActiveRecord中使用的find

Friday, May 11th, 2007

首先不要用find helper,也就是那些find_by_colName,效率差

第二,像直接写SQL一样做tunning,不要简单的find(:all),仔细看最后生成的SQL,要额外关注:joins, :include, :order, :select
利用:join, :include可以完成连接,从效率上看很有必要,虽然用起来稍微麻烦点。完全以来与ActiveRecord生成的object relationship的话代码写起来很爽,但是会多出很多的SQL.

:order很简单,但是仔细看系统会根据model里面的has_, belong_关系为你多加上一个order by

不用:select就相当于select *,当你的table中有大量文本的时候这个很恐怖

此外也不要过多的依赖find_by_sql, 这样会很大程度上破坏ActiveRecord完成的mapping工作, 尤其是在项目前期。这样的sql tunning还是留到后面的重构中吧,起码等到这个需要确定要保留下来的时候:)

使用ORMapping后数据库设计的问题

Thursday, May 10th, 2007

之前系统中直接保存post的title和description, 在显示的时候要对title对一个strip操作,比如将Free Memory修改为free-memory,用于friendly url. 将description做截断用于列表中的显示.

现在发现这样是完全不靠谱的, 首先上面提到的两个运算会发生在每次请求过程中, 虽然可能耗时不多, 但是在一个开放的Web系统中这样的开销仍然是一种罪恶!

其次的问题发生在ORMapping的使用中, 参考typo的方法, Post model中有一个stripped_title的方法, 用于生成stripped title, 而产生的问题是今后我的操作必须严格依照object relationship, 无法进行SQL scale. 比如我想取所有的comments, 顺便要left join posts, 这样stripped title将无法得到….如果使用comment.post.stripped_title看似很爽,但是在log中很明显可以看出SQL多了几条, 这样的开销是不被允许的.

最后的办法就是在执行New, Update操作时一定要直接生成 stripped title, excerpt并保存在数据库中. 切记切记 TOT