简体   繁体   English

如何使用Rails控制器页面将数据插入到具有多对多关系的联接表中

[英]How can I Insert Data Into a Join Table with a Many to Many Relation Using a Rails Controller Page

Given: 2 Tables, Lists, and Cars, which have many to many relations. 给出:2个表,列表和汽车,它们之间有许多关系。

I want to append it with data from console. 我想从控制台添加数据。 Finding and reading it from the browser works, but how can I add and append through the rails controller page? 从浏览器中查找和读取它是可行的,但是如何通过Rails控制器页面添加和追加呢?

With any has_many relationship you can add a record with << 通过任何has_many关系,您都可以添加带有<<的记录

@car.lists << @list
@list.cars << @car

You should use this in your cad controller: 您应该在cad控制器中使用它:

lists = List.where(id:params[:lists])
@cars.assign_attributes({lists: lists})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM