设置了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的更多相关文章

  1. MySql_delete同时删除多表相关联记录

    sql delete同时删除多表相关联记录 sqlserver 支持级联更新和删除oracle 只支持级联删除 删除包含主键值的行的操作,该值由其它表的现有行中的外键列引用.在级联删除中,还删除其外键 ...

  2. 【Head First Servlets and JSP】笔记22:直接从请求到JSP & 获取Person的嵌套属性

    直接从请求到JSP,不经过servlet <!DOCTYPE html> <html lang="en"> <head> <meta ch ...

  3. mybatis 关联查询和嵌套查询的简单示例

    两个表: Customer 顾客表 create table if not exists customer( customer_id int primary key auto_increment, f ...

  4. Mybatis关联查询(嵌套查询)

    上一篇文章介绍了基于Mybatis对数据库的增.删.改.查.这一篇介绍下关联查询(join query). 三张表:user article blog 表的存储sql文件: /* Navicat My ...

  5. yii2 ActiveRecord多表关联以及多表关联搜索的实现

    作者:白狼 出处:http://www.manks.top/yii2_many_ar_relation_search.html 本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明 ...

  6. JavaScript Nested Function 的时空和身份属性

    JavaScript 的function 不仅仅是一等公民,简直就是特殊公民.它有许多独特的特征: 1) 它是object,可以存储,传递,附加属性. 2) 它可以有lexical closure, ...

  7. 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. ...

  8. Dynamics CRM2016 Web API之更新记录的单个属性字段值

    在web api中提供了对单个属性的更新接口,这和查询中查询单个属性类似,对这个接口我个人也是比较喜欢的. var id = "{D1E50347-86EB-E511-9414-ADA183 ...

  9. spring BeanWrapperImpl方便的嵌套属性(list)操作

    beans 包主要提供了接口和类用于处理java beans.     其中最主要的接口是BeanWrapper:     Spring 的中心接口,用于访问javabeans 的低层操作.默认实现为 ...

随机推荐

  1. Fiddler设置断点修改Request和Response

    一.Fiddler中修改Request有两种方法:  点击Rules-> Automatic Breakpoint ->Before Requset (这种方法会中断所有的会话) 消除命令 ...

  2. springboot中通过cors协议解决跨域问题

    1.对于前后端分离的项目来说,如果前端项目与后端项目部署在两个不同的域下,那么势必会引起跨域问题的出现. 针对跨域问题,我们可能第一个想到的解决方案就是jsonp,并且以前处理跨域问题我基本也是这么处 ...

  3. 编写Shell脚本的最佳实践

    编写Shell脚本的最佳实践 http://kb.cnblogs.com/page/574767/ 需要记住的 代码有注释 #!/bin/bash # Written by steven # Name ...

  4. Django中CBV源码解析

    使用 关于FBV和CBV的使用在之前有提到,点击穿越. 准备 首先在视图中创建一个类并继承 django.views.View 类,在类中可定义各种请求方式对应执行的函数(函数名为请求方式名称小写). ...

  5. GDB查看堆栈局部变量

    GDB查看堆栈局部变量 “参数从右到左入栈”,“局部变量在栈上分配空间”,听的耳朵都起茧子了.最近做项目涉及C和汇编互相调用,写代码的时候才发现没真正弄明白.自己写了个最简单的函数,用gdb跟踪了调用 ...

  6. Python 运维

    1.python解释器提供提供的小工具 1.1 一秒钟启动一个下载服务器 进入要下载文件的目录(shift+鼠标右键可以很快的在当前目录打开一个cmd) python2: python2 -m Sim ...

  7. 监听器&上传下载&I18N

    监听器(Listener) 监听Java对象 的方法调用和属性改变() web的一个组件 事件驱动编程:事件源,事件名称,事件响应函数,事件对象 以后在Spring中的配置 WEB中有哪些监听器?  ...

  8. Python基础(十三) 为什么说python多线程没有真正实现多现程

    Python中的多线程没有真正实现多现程! 为什么这么说,我们了解一个概念,全局解释器锁(GIL). Python代码的执行由Python虚拟机(解释器)来控制. Python在设计之初就考虑要在主循 ...

  9. django中的数据库迁移

    Django是用python写的web开发框架,其特点是: 1.重量级框架,内部封装了很多的功能组件,使开发变的简便快速, 2.MVT模式:前后端分离,高内聚低耦合,m:model,与mvc中的m功能 ...

  10. C# AsyncCallback异步回调用法示例

    using System;using System.Collections.Generic;using System.Linq;using System.Text;using System.Threa ...