Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8672509
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T19:13:23+00:00 2026-06-12T19:13:23+00:00

If anyone has a better title, please let me know :p I have the

  • 0

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?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T19:13:24+00:00Added an answer on June 12, 2026 at 7:13 pm

    The following is going to create new instance of Car, but does not associate it with the Driver instance.

    driver_object.cars.create
    

    The following works

    driver_object.cars << Car.create(...)
    

    The << method in ActiveRecord appends the newly created Car instance to the :cars collection on Driver and calls save on Driver, creating the CarDriver instance to relate the new Car with driver_object.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

If anyone has a better title, let me know. I made a DisposeHelper so
Please excuse the vague title. If anyone has a suggestion, please let me know!
I wasn't sure what the best title is so if anyone has a better
Sorry for the vague title, if anyone can think of a better one please
That may be a shitty title. If anyone has a better idea for how
I could not make a better title, if anyone can do it please, help
Please excuse the odd title, but I don't know how better to describe my
As the title says, has anyone (preferably a well established library) got a better
Does anyone has any insight on organizing sqlalchemy based projects? I have many tables
Let's assume that we have a PHP class Page , which generates PHP Page

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.