Skip to content

Instantly share code, notes, and snippets.

View liwh's full-sized avatar
🎯
Focusing

robie lee liwh

🎯
Focusing
View GitHub Profile
>> New.find(1)
ActiveRecord::RecordNotFound: Couldn't find New with ID=1
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1591:in `find_one'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:1574:in `find_from_ids'
from /usr/lib/ruby/gems/1.8/gems/activerecord-2.3.2/lib/active_record/base.rb:616:in `find'
from (irb):4
>> New.find_by_id(1)
=> nil
//直接利用find方式查找,如果没有数据会跑出异常,而利用find_by_id方式会返回nil
ActionRecord 配置
整理出来方便查询
1.ActionRecord:Base.logger=logger
接受一个logger对象。改对象被内部使用。用于记录数据库的操作。如果应用程序须记录数据库操作,也会使用这个logger对象。
2.ActionRecord:Base.primary_key_prefix_type=option
如果option为nil.则所有表的缺省主键字段都是id,如果改选项值为:table_name,则会在"id"前面加上表名,如果改选值 为:
table_name_with_underscore,则会在表名与id之间以下画线分隔。
3.ActionRecord:Base.table_name_prefix="prefix"
在表名前面加上指定的字符串。譬如说,如果模型类叫做User,前缀字符串是"myapp-",则Rails会寻找名为"myapp- users"的表,如果
需要与别的应用程序共享数据库,或者必须用同一个数据库进行并发开发与测试,这个选项就能派的上用场。
//Hash to Struct
class Hash
def to_struct(struct_name)
Struct.new(struct_name,*keys).new(*values)
end
end
if $0 == __FILE__
h = {:name=>"Dan","age"=>33,"rank"=>"SrA","grade"=>"E4"}
class Hash
def method_missing(method, *params)
#首先得把symbol转化成string,如h={}.h.a = 1,此时,method 就为:a=
method_string = method.to_s
if method_string[-1] == 61
self[method_string[0..-2]] = params.first
else
self[method_string]
end
end
获取当前IP地址:ifconfig eth0 |grep "inet addr:" |awk '{print $2}'|cut -c 6-
module MyModule
module ClassMethods
def act_as_my_module(params)
#Note that in order to avoid having to escape string delimiters, we use a #special, multiline string syntax. Text between <<-DELIM ... DELIM is treated as a #plain old string. In this case, our string is Ruby code intended to be evaluated by #class_eval.
class_eval do <<-DELIM
def some_method
#remember , string substitution
#is OK here. e.g., #{params[:foo]}
end
DELIM

sudo kill -9 `ps -A | grep mem | awk ‘{print $1}’` && memcached -m 20 -d
//返回某个时候的改动
git reset —soft …
git reset —hard …

git reset 回退以前某个版本

git reset是指将当前head的内容重置,不会留log信息。
    * git reset HEAD filename  从暂存区中移除文件
@liwh
liwh / gist:633930
Created October 19, 2010 09:43
factory_girl error
Factory.define :valid_thing, :class => Thing do |t|
  t.name 'Some valid thing'
  t.user { Factory(:valid_user) }
end
Wrapping the call in {} causes Factory Girl to not evaluate the code inside of the 
braces until the :valid_thing factory is created. This will force it to wait until the
@liwh
liwh / gist:652752
Created October 29, 2010 02:10
快速添加到每一个文件的方法
find app -iname '*.rb' | xargs sed -i '1 i\# encoding: utf-8'
后台启动mongodb: ./mongod --fork --logpath /var/log/mongodb.log --logappend
关闭mongodb: $ ./mongo
> db.shutdownServer()
今天,准备执行rake db:seed时,发现出现:no such file to load --openssl,
在安装了sudo apt-get install openssl libopenssl-ruby1.9 都没有效果后,此时,从http://openssl.org/ 上下载OpenSSL 0.9.8o 的tar包,自己进行编译安装。步骤如下。
1. tar -xvf openssl*
2. cd openssl*
3. sudo make && sudo make install
4.进入ruby源文件目录,找到/ext/openssl 的目录,进入此目录