ruby on rails

 最好的参考资料:ruby文档 http://guides.ruby-china.org

CRUD:

C:user = User.create(name:"David", occupation: "Code Artist") 或者

user = User. new
user.name = "David"
user.occupation = "Code Artist"
user.save

R: 
users=User.all
    user=User.first
nemo=User.find_by(name:'Nemo')
users = User.where(name: 'David' , occupation: 'Code Artist' ).order( 'created_at DESC' )

详见: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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值