Archive for ‘Rails’ Category

26
Dec

hack truncate in ruby 1.8.7 compability issue

add follows code into rails app environment.rb and restart the service
# http://rails.lighthouseapp.com/projects/8994/tickets/867-undefined-method-length-for-enumerable

class String
def chars
ActiveSupport::Multibyte::Chars.new(self)
end
alias_method :mb_chars, :chars
end
##
module ActionView
module Helpers
module TextHelper
def truncate(text, length = 30, truncate_string = “…”)
[...]

20
Nov

comparison of common command with Rails and Merb


04
Nov

Thinking Sphinx on Windows

In one of my Ruby on Rails projects, I needed a full-text search engine to integrate. And after some research, I decided that Thinking Sphinx is the way to go. It seemed simple, fast and well documented. Unfortunately, I had some issues when tried to get it running on Windows.
The first step is installing [...]

25
Oct

rails store format in memcache

在Rails开发过程中,经常需要查看Memcache的存储内容,原来一直通过console方式,但速度太慢,效果不好,今天试图用Telnet去查看。说干就干
telnet 127.0.0.1 11211
get ‘user_login’
ERROR
奇怪,不应该呀,这个Key明显是存到Cache里了,重新打开console模式测试,可以正常取出。这里有点怀疑Rails写入Memcache的机制,是不是加了类似保存Session时的前缀啊。
怎么直接查看Memcache已保存的所有Key,Google之发现使用 stats cachedump slab_id limit_num可以直接查看Memcache某个slab中的前limit_num个key列表,显示格式如下
ITEM key_name [ value_length b; expire_time|access_time s]
嗯,很爽,果不出所料,Rails确实在存入Memcache前做了点手脚,加了一个前缀。现在将前缀加上再取,成功!
stats cachedump 1 10
ITEM active_record:friends:86799 [1 b; 1224840079 s]
ITEM p1_cn-production:ticker_key [4 b; 1224839825 s]
ITEM p1_cn-production:timer_key [4 b; 1224839825 s]
get p1_cn-production:active_record:friends:86799
VALUE p1_cn-production:active_record:friends:86799 0 75
[iP?i恑a?i}?i劽i惷i旅i?i竺i0?iK?i i~igit?i睹
可以看出前缀是根据Rails项目名称和运行模式生成的,还挺聪明

20
Sep

use fixture in rails 2

以往在使用Fixture时都是用部分数据填充来模拟达到真实效果,但Model之间的关系使的维护与书写Fixture成为一件比较复杂的事情,稍有不慎,Fixture也会使测试代码变的十分脆弱,今天看到一个不错的方式,基本上可以解决这个问题.方法如下:

# products.yml
couch:
name: Couch
price: 399.99
manufacturer: lazyboy
categories: furniture
tv_stand:
name: TV Stand
price: 149.95
manufacturer: highdeph
categories: furniture, electronics
# manufacturers.yml
lazyboy:
name: LazyBoy
highdeph:
name: HighDeph
# categories.yml
furniture:
name: Furniture
electronics:
name: Electronics

感兴趣的朋友还可以看视频教程,相信会有所收获
video tutorial

19
Sep

rails commands

rails command list
# rake db:fixtures:load
# # 载入fixtures到当前环境的数据库
# # 载入指定的fixtures使用FIXTURES=x,y
# rake db:migrate
# # 迁移数据库通过在db/migrate目录下的脚本.可以指定版本号通过VERSION=x
# rake db:schema:dump
# # 创建一个db/schema.rb文件,通过AR能过够支持任何数据库去使用
# rake db:schema:load
# # 载入一个schema.rb文件进数据库
# rake db:sessions:clear
# # 清空sessions表

    About

    Tag Cloud