ActiveRecord Nested Atrributes 关联记录,对嵌套属性进行CURD
设置了Nested attributes后,你可以通过父记录来更新/新建/删除关联记录。
使用: #accepts_nested_attributes_for class method。
例如:
class Member < ActiveRecord::Base
has_one :author
has_many :posts
accepts_nested_attributes_for :author, :posts
end
于是就增加了2个方法:XXX_attributes=(attributes)
分为2种情况:
- one to one
- ont to many
一对一的情况,生成的参数就是嵌套hash.
params = { member: { name: 'Jack', author_attributes: { icon: 'smiling' } } }
一对多的情况,参数包含了key :posts_attributes和它的属性,一个数组的hashes作为value。
params = { member: {
name: 'joe',
posts_attributes: [
{ title: 'Kari, the awesome Ruby documentation browser!' },
{ title: 'The egalitarian assumption of the modern citizen' },
{ title: '', _destroy: '1' } # this will be ignored
]
}} 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' 在 controller中的参数验证方法需要加上:
params.require(:member).permit(:name, posts_attributes: [:title, :_destroy]) _destroy:'1'是当加上参数all_destroy: true后,用于删除所有关联的选项。一对多,也可以使用嵌套hash代替:
params = { member: {
name: 'joe',
posts_attributes: {
first: { title: 'Kari, the awesome Ruby documentation browser!' },
second: { title: 'The egalitarian assumption of the modern citizen' },
third: { title: '', _destroy: '1' } # this will be ignored
}
}}
options
具体用法见api
ActiveRecord Nested Atrributes 关联记录,对嵌套属性进行CURD的更多相关文章
- MySql_delete同时删除多表相关联记录
sql delete同时删除多表相关联记录 sqlserver 支持级联更新和删除oracle 只支持级联删除 删除包含主键值的行的操作,该值由其它表的现有行中的外键列引用.在级联删除中,还删除其外键 ...
- 【Head First Servlets and JSP】笔记22:直接从请求到JSP & 获取Person的嵌套属性
直接从请求到JSP,不经过servlet <!DOCTYPE html> <html lang="en"> <head> <meta ch ...
- mybatis 关联查询和嵌套查询的简单示例
两个表: Customer 顾客表 create table if not exists customer( customer_id int primary key auto_increment, f ...
- Mybatis关联查询(嵌套查询)
上一篇文章介绍了基于Mybatis对数据库的增.删.改.查.这一篇介绍下关联查询(join query). 三张表:user article blog 表的存储sql文件: /* Navicat My ...
- yii2 ActiveRecord多表关联以及多表关联搜索的实现
作者:白狼 出处:http://www.manks.top/yii2_many_ar_relation_search.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明 ...
- JavaScript Nested Function 的时空和身份属性
JavaScript 的function 不仅仅是一等公民,简直就是特殊公民.它有许多独特的特征: 1) 它是object,可以存储,传递,附加属性. 2) 它可以有lexical closure, ...
- LeetCode 339. Nested List Weight Sum (嵌套列表重和)$
Given a nested list of integers, return the sum of all integers in the list weighted by their depth. ...
- Dynamics CRM2016 Web API之更新记录的单个属性字段值
在web api中提供了对单个属性的更新接口,这和查询中查询单个属性类似,对这个接口我个人也是比较喜欢的. var id = "{D1E50347-86EB-E511-9414-ADA183 ...
- spring BeanWrapperImpl方便的嵌套属性(list)操作
beans 包主要提供了接口和类用于处理java beans. 其中最主要的接口是BeanWrapper: Spring 的中心接口,用于访问javabeans 的低层操作.默认实现为 ...
随机推荐
- 全文索引&&地理空间索引
Ⅰ.全文索引 搜索引擎的实现核心技术,搜索类似where col like '%xxx%';关键字可以出现再某个列任何位置 这种查询条件,B+ tree索引是无法使用的.如果col上创建了索引,因为排 ...
- maven跳过单元测试-maven.test.skip和skipTests的区别
1. 介绍 -DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下. -Dmaven.test.skip=true,不执行测试用例, ...
- java框架之SpringCloud(7)-Config分布式配置中心
前言 分布式系统面临的配置问题 微服务意味着要将单体应用中的业务拆分成一个个子服务,每个服务的粒度相对较小,因此系统中标会出现大量的服务.由于每个服务都需要必要的配置信息才能运行,所以一套集中式的.动 ...
- element-ui table表格展开行每次只能展开一行
https://www.jianshu.com/p/a59c22202f2c <template> <el-table @expand-change="expandSele ...
- Eclipse集成scala插件
1.Eclipse中右击help,选择Eclipse Marketplace,搜索scala,一路点击安装,重启Eclipse. 2.新建工程,new->other->出现scala wi ...
- Hbase 读写 原理
客户端读取信息流程 ()client要读取信息,先查询下client 端的cache中是否存在数据,如果存在,刚直接返回数据.如果不存在,则进入到zookeeper,查找到里面的相应数据存在的Root ...
- 灵雀云容器PaaS平台助力知名股份制银行金融科技革新
互联网.科技和金融的碰撞给银行业带来巨大影响.IT技术起初是传统金融提升效率的工具和方法,随着新技术的演进,技术成为驱动变革的核心要素.Fintech金融科技以技术和数据为驱动,用创新的方法改变了金融 ...
- 为archlinux终端ls不同类型文件设置不同显示颜色
title: 为archlinux终端ls不同类型文件设置不同显示颜色 date: 2017-11-13 20:53:55 tags: linux categories: linux archlinu ...
- debug调试命令
- ASP.NET MVC案例教程(二)
ASP.NET MVC案例教程(二) 让第一个页面跑起来 现在,我们来实现公告系统中的第一个页面——首页.它非常简单,只包括所有公告分类的列表,并且每个列表项是一个超链接.其中分类数据是用我们的Moc ...