ruby on rails 开发笔记

ruby on rails 开发笔记

常用命令

  • 创建模型:rails g model Student(单数) name:string age:integer

  • 创建controller:rails s controller Students(复数)

routes

controller

model

  • 根据规范,创建的模型类是大写字母开头,驼峰格式的单数,比如IrspInfo,数据库中创建的表,是下划线风格的复数形式,比如irsp_info

  • curd语法:

# create方式1
user = User.create(name:"yajima",age:19,city:"shanghai")
# create方式2
user = User.new(name:"yajima",age:19,city:"shanghai")
user.save
# create方式3
user = User.new
user.name = "yajima"
user.age = 19
user.city = "shanghai"
user.save

# read
users = User.all
user = user.first
# find_by返回查到的第一个
user = User.find_by(name: "yajima")
# where 返回查到的所有
users = User.where(name: "yajima",age: 18).order(age: :desc)


# update 方式1
user = User.find_by(name: "yajima")
user.name = "suzuki"
user.save

# update 方式2
user = User.find_by(name: "yajima")
user.update(name: "suzuki")
# 全量更新
User.update(:all,name: "newName", age: 50)


# delete 方式1
user = User.find_by(name: "yajima")
user.destroy
# delete 方式2
# 删除所有满足条件的
User.destroy_by(name: "yajima")
  • validation语法:
validates(:name,{presence: true})
# save,create,update以及对应!的方法才会valid,带!的方法会跑异常,不然只是更新失败
# 出错的信息都保存在对象的errors数组中
p= Person.new 
p.errors.size # 0
p.valid?      # false

## 常见的valid helper
validates(:start_date, {comparison: {greater_than: Time.now}})
validates(:age, {comparison: {greater_than_or_equal_to: 20}})

validates(:username, {exclusion: {in: ["yajima","suzuki"]}, message: "%{value} is in black list" })  # same as inclusion

validates(:email, {format: {with: /\A[a-zA-Z]+\z/, message: "email format error" }})

validates(:name, {length: {maximun: 20} })
validates(:name, {length: {minimum: 10} })
validates(:name, {length: {in: 10..20} })
validates(:name, {length: {is: 16} })

validates(:score, {numericality: true})  # can be float
validates(:score, {numericality: {only_integer: true}}) ## same as :greater_than, :in, :odd , :even


validates(:username, {uniqueness: true})
  • 自定义validates方法
class MyValidator < ActiveModel::Validator
  def validate(record)
    if record.first_name = "yajima"
      record.errors.add(:base,"This person is evil")
    end
  end
end

class Person < ApplicationRecord
  validates_with MyValidator
end
## 带options的validates方法
class MyValidator < ActiveModel::Validator
  def validate(recode)
    if options[:fields].any? {|field| record.send(field) == "Evil"}    ## 注: record.send(field) 就是 record[field]的用法,就是动态获取字段的值的写法。
      record.errors.add(:base,"This person is evil")
    end
  end
end

class Person < ApplicationRecord
  validates_with MyValidator, fields: [:first_name, :last_name]
end

## 如果特别复杂,可以用以下写法
class Person < ApplicationRecord
  validate do |person|
    GoodnessValidator.new(person).validate
  end
end

class GoodnessValidator
  def initialize(person)
    @person = person
  end

  def validate
    if some_complex_condition_involving_ivars_and_private_methods?
      @person.errors.add :base, "This person is evil"
    end
  end
end

# 通过block来validate
class Person < ApplicationRecord
  validates_each :username do |record, attr, value|
    if value.do_somthing
      record.errors.add(attr,"recode attr cur value #{value} is error")
    end
  end
end

migrate

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值