hash ruby 定义 嵌套_【译】rails的嵌套属性(Nested Attributes)使用
一个member有一些post
class Member < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :posts
end
你可通过属性hash来增加或更新关联post model
每一个不含有id键的新记录会被实例化,除非该hash也包含了一个 :_destroy => true
params = {
:member => {
:name => 'joe', :posts_attributes => [
{ :title => 'Kari, the awesome Ruby documentation browser!' },
{ :title => 'The egalitarian assumption of the modern citizen' },
{ :title => '', :_destroy => '1' } # 该记录会被忽略
]
}
}
member = Member.create(params['member'])
member.posts.length # => 2
member.posts.first.title # => 'Kari, the awesome Ruby documentation browser!'
member.posts.second.title # => 'The egalitarian assumption of the modern citizen'
通过 :reject_if proc设置忽略的不满足条件的记录。例如:
class Member < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :posts, :reject_if => proc { |attributes| attributes['title'].blank? }
end
params = { :member => {
:name => 'joe', :posts_attributes => [
{ :title => 'Kari, the awesome Ruby documentation browser!' },
{ :title => 'The egalitarian assumption of the modern citizen' },
{ :title => '' } # 这个记录会被忽略
]
}}
member = Member.create(params['member'])
member.posts.length # => 2
member.posts.first.title # => 'Kari, the awesome Ruby documentation browser!'
member.posts.second.title # => 'The egalitarian assumption of the modern citizen'
:reject_if也可一接受一个symbol来代表一个可用方法:
如果hash中含有一个id和已有的关联记录相匹配,则被匹配到的记录会被修改:
member.attributes = {
:name => 'Joe',
:posts_attributes => [
{ :id => 1, :title => '[UPDATED] An, as of yet, undisclosed awesome Ruby documentation browser!' },
{ :id => 2, :title => '[UPDATED] other post' }
]
}
member.posts.first.title # => '[UPDATED] An, as of yet, undisclosed awesome Ruby documentation browser!'
member.posts.second.title # => '[UPDATED] other post'
默认关联记录是被保护的(不被删除)。如果想通过属性hash来删除任何关联记录,你需要打开:allow_destroy选项,这样使用_destroy键来删除记录就行:
class Member < ActiveRecord::Base
has_many :posts
accepts_nested_attributes_for :posts, :allow_destroy => true
end
params = { :member => {
:posts_attributes => [{ :id => '2', :_destroy => '1' }]
}}
member.attributes = params['member']
member.posts.detect { |p| p.id == 2 }.marked_for_destruction? # => true 标记将被删除
member.posts.length # => 2 未保存,所以还没删除
member.save
member.reload.posts.length # => 1
hash ruby 定义 嵌套_【译】rails的嵌套属性(Nested Attributes)使用相关推荐
- if函数和and函数嵌套_在Excel中嵌套IF函数
if函数和and函数嵌套 The IF function lets you test something in Excel, and show one result if the test resul ...
- python 避免多重条件嵌套_如何避免多层嵌套函数
我使用的python代码有4个层次的嵌套函数.您可以看到以下示例:class TestClass: def __init__(self): pass def calculate(self, x, y) ...
- java三元表达式嵌套_三元运算符的嵌套详解:分别在JSTL、JavaScript和Java中
用来完成简单的选择逻辑,即根据条件判断,从两个选择中选择一种执行. 使用格式: (条件表达式)?表达式1:表达式2: 运算规则:条件表达式 值为一个布尔值,当值为true的时候 执行表达式1 否 ...
- python set 嵌套_带有路径的嵌套dict中的Python setitem
程序中有几个语法错误和一个逻辑错误.在 逻辑错误是您使用sample_dict.你有for ...: temp_dict = sample_dict.get(...) 应该是的 ^{pr2}$ 这是你 ...
- ruby 代码调试_用撬调试Ruby代码
ruby 代码调试 Your comprehensive guide to Pry and how to use it . 有关Pry及其使用方法的综合指南. Every programmer has ...
- rails pry使用_在Rails 6中使用Gritter
rails pry使用 介绍 (Introduction) Hello everyone, how has it been working with the challenges of COVID-1 ...
- insert exec 语句不能嵌套_走向DBA「MSSQL篇」 - 从SQL语句的角度提高数据库的访问性能...
最近公司来一个非常虎的DBA,10几年的经验,这里就称之为蔡老师吧,在征得我们蔡老同意的前提下 ,我们来分享一下蔡老给我们带来的宝贵财富,欢迎其他的DBA来拍砖. 目录 1.什么是执行计划?执行计划是 ...
- [译] D3.js 嵌套选择集 (Nested Selection)
译者注: 原文: Mike Bostock (D3.js 作者) -- Nested Selections 译者: ssthouse 本文讲解的是关于 D3.js 中 d3-selection 的使用 ...
- 选择嵌套_如何优雅地在JavaScript中访问嵌套对象
在JavaScript中访问嵌套对象 以超酷的方式安全地访问JavaScript中的嵌套对象 JavaScript是惊人的,我们都知道.但是JavaScript中的一些东西真的很奇怪,它们让我们大开眼 ...
最新文章
- 手机远程ssh登录Linux,Linux SSH(远程登录)到Milestone手机
- lambda表达式初步
- www.python123.org_python爬虫-requests
- docker 覆盖 entrypoint_跟我一起学docker(八)--Dockerfile
- mysql5.5和5.6版本间的坑
- 收缩sqlserver事务日志
- 什么叫侧面指纹识别_屏幕指纹技术最强的3款全面屏手机,指纹识别技术手机你喜欢吗?...
- jieba 词典 词频_python jieba分词(添加停用词,用户字典 取词频
- 控制台打印_星星助手1S·便携式智能液晶电视串口工具 打印信息查看指令发送...
- Procez One 快速入门(4)
- RGB在线取色器,可视化三通道颜色
- C语言 分解质因数。例如:输入90,打印出90=2*3*3*5。
- 冬令时 java_java-时间处理夏令时冬令时跨时区问题处理
- dot com过时了,个性域名“钱”景看好
- pthread编译时报错的解决方法
- mt950报文解析_MT9**报文学习
- 50个好网站,从此上网不再虚度!
- 基于阿里云ECS,Ubuntu系统搭建离线下载服务器
- VVC帧内预测(一)
- workbench前处理分块画分网格
热门文章
- 数据采集技术的难点在于哪里
- 如何选择合适的大数据分析平台的类型
- opencv检测相交点_OpenCV学习笔记(八) 边缘、线与圆的检测
- php数据字段多怎么存,一个保存时间的字段怎么存储数据
- 黑苹果 2分钟 重启_一分钟了解苹果iPad Pro 2020的激光雷达到底是什么黑科技?...
- AcWing 884. 高斯消元解异或线性方程组
- 2021年SWPUACM暑假集训day4KMP算法
- AtCoder Beginner Contest 185
- 2019华北五省计算机应用大赛官网,“远洋航空杯”2019年华北五省(市、自治区) 及港澳台大学生计算机应用大赛举行...
- 第六章_循环神经网络(RNN)