简体   繁体   中英

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.

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?

With any has_many relationship you can add a record with <<

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

You should use this in your cad controller:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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