Rails 5中belongs_to默认不允许nil
来源:互联网 发布:游戏制作软件中文版 编辑:程序博客网 时间:2024/05/16 17:17
Rails 5中默认情况belongs_to为nil不可创建
class User < ApplicationRecordendclass Post < ApplicationRecord belongs_to :userendpost = Post.create(title: 'Hi')=> <Post id: nil, title: "Hi", user_id: nil>post.errors.full_messages.to_sentence=> "User must exist"
在Rails 4中
class User < ApplicationRecordendclass Post < ApplicationRecord belongs_to :user, required: trueendpost = Post.create(title: 'Hi')=> <Post id: nil, title: "Hi", user_id: nil, created_at: nil, updated_at: nil>post.errors.full_messages.to_sentence=> "User must exist"
Rails 4默认required: false
如果想在Rails 5中想要允许nil, 需要添加optional: true
class Post < ApplicationRecord belongs_to :user, optional: trueendpost = Post.create(title: 'Hi')=> <Post id: 2, title: "Hi", user_id: nil>
阅读全文
0 0
- Rails 5中belongs_to默认不允许nil
- rails 5 关于修改数据之后配置了belongs_to :doctor如果doctor_id不填一直报错rollback transaction
- 【Rails】inverse_of在has_many和belongs_to中的用法
- 理解RoR(Ruby on Rails)中 nil? empty? blank?方法的区别
- RAILS关于 NIL? PRESENT? EMPTY? ANY? BLANK?
- Rails 3 修改url中默认的的/:id
- iOS 中 nil, Nil, NULL 理解区分
- OC中nil/Nil/NULL的区别
- IOS中nil/Nil/NULL的区别
- iOS中nil,Nil,NULL的区别
- ios中NULL nil Nil的区别
- OC中nil,Nil,NULL的区别
- iOS中nil、Nil、NULL、NSNull 区别
- iOS中nil 、NULL、 Nil 、NSNull
- Go中nil
- Rails 5中使用Bootstrap
- Understanding nil?, empty? and blank? in Ruby and Rails
- ruby on rails错误undefined method `title' for nil:NilClass
- 教程篇(5.4) NSE4 03. 防火墙策略 ❀ 飞塔 (Fortinet) 网络安全专家
- Android 打印当前线程的调用堆栈
- 前台开发总结11——20171115
- android设置颜色方法
- python 学习好博客
- Rails 5中belongs_to默认不允许nil
- 非负随机变量X满足:(1-F(x)) 在 (0,+∞)积分为= E[X]
- day13 正则表达式、collection、list
- RPG简单角色加载
- useBodyEncodingForURI="true"解决Tomcat乱码问题
- oracle 查询并清除锁lock
- 循环中标签中使用 break outer continue outer
- Android 5.1设置默认输入法
- android 自定义圆形图片