最好的参考资料:ruby文档 http://guides.ruby-china.org
CRUD:
C:user = User.create(name:"David", occupation: "Code Artist") 或者
详见:http://guides.ruby-china.org/active_record_basics.html
数据验证:p=Person.new
validates
:name
, presence:
true #是否存在
p.valid?来进行测试,错误信息存储在p.errors.messages
errors[:attribute].any? 验证某个属性是否报错
validates
:terms_of_service
, acceptance: { accept:
'yes'
} #决定接受什么值,默认为'1'
class
Library < ActiveRecord::Base
has_many
:books
validates_associated
:books
end
#验证关联对象,只需在one上写vali....
validates
:legacy_code
, format: { with: /\
A
[a-zA-
Z
]+\z/,
message:
"only allows letters"
}
validates
:subdomain
, exclusion: {
in
: %w(www us ca jp),
message:
"%{value} is reserved."
}
validates
:size
, inclusion: {
in
: %w(small medium large),
message:
"%{value} is not a valid size"
}
validates
:name
, length: { minimum:
2
}
validates
:bio
, length: { maximum:
500
}
validates
:password
, length: {
in
:
6
..
20
}
validates
:registration_number
, length: { is:
6
}
详见:http://guides.ruby-china.org/active_record_validations.html
migrate的用法:http://blog.csdn.net/felomeng/article/details/21121509
新建:$ rails g model event name:string description:text is_public:boolean capacity:integer
新建一系列包括m,v,c:$ rails g scaffold person name:string bio:text birthday:date
停止服务器:lsof |grep 3000 然后kill pid 20409