If anyone has a better title, please let me know :p
I have the following models:
class Car
has_many :car_drivers
has_many :drivers, :through => :car_drivers
end
class Driver
has_many :car_drivers
has_many :cars, :through => :car_drivers
end
class CarDriver
belongs_to :car
belongs_to :driver
end
Now I want to create a new Driver via Car, but the record in the join-table (car_drivers) should be created as well. I tried the following, but while the car record is created, the join-table record is not: driver_object.cars.create
What is the best practice in this case?
The following is going to create new instance of
Car, but does not associate it with theDriverinstance.The following works
The
<<method in ActiveRecord appends the newly createdCarinstance to the:carscollection onDriverand callssaveonDriver, creating theCarDriverinstance to relate the newCarwithdriver_object.