peterateftawfik
peterateftawfik

Reputation: 57

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?

Upvotes: 0

Views: 59

Answers (2)

Will Sheehan
Will Sheehan

Reputation: 25

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

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

Upvotes: 1

Kaushlendra Tomar
Kaushlendra Tomar

Reputation: 1440

You should use this in your cad controller:

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

Upvotes: 0

Related Questions