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

  • Home
  • SEARCH
  • 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 8395201
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T20:14:28+00:00 2026-06-09T20:14:28+00:00

I have three models class Vehicle < ActiveRecord::Base has_one:driver ,:through=>:vehicle_driver end class Vehicle_Driver <

  • 0

I have three models

class Vehicle < ActiveRecord::Base
 has_one:driver ,:through=>:vehicle_driver
end

class Vehicle_Driver < ActiveRecord::Base
 belongs_to:vehicle
 belongs_to:driver
end

have only vehicle_id and driver_id

class Driver < ActiveRecord::Base
 has_one:vehicle_driver
end

I want to do is

  • register the vehicles independently

  • register the Drivers independently

  • then assign driver to vehicle such that only one driver can be assigned to one vehicle and vice versa

and further

  • if I assign driver to another vehicle its early relation must be deleted

Is this possible from above relationship …?????

Any help will be appreciated..

Thanks In Advance

  • 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-09T20:14:29+00:00Added an answer on June 9, 2026 at 8:14 pm
    1. Why the intermediate class, it contradicts your requirements, so remove it.

    2. So just let both models Vehicle and Driver use has_one of the other. It generates a duplicate relationship that could lead to problems, if they are not pointing to each other. So the best is to have only one ‘has_one’-relationship, and do @vehicle.find_by_driver to test if a driver already has a vehicle assigned to it.

    3. When you save a relationship, also update the other relation:
      3a @vehicle.driver = @driver
      3b @vehicle.driver.vehicle = @vehicle # to force make them point to each other.

    4. In the Vehicle model, add a method or scope to return a list of vehicles where driver is nil.
      4a Vehicle.all.where(:driver.nil?) # from my head, just to give an idea

    If you want to remove a relationship for a vehicle, do
    5a @vehicle.driver.vehicle = nil
    5b @vehicle.driver = nil

    Hope this helps you in the right direction.

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

Sidebar

Related Questions

I have three models: class ReleaseItem < ActiveRecord::Base has_many :pack_release_items has_one :pack, :through =>
I have three models: class Book < ActiveRecord::Base has_many :collections has_many :users, :through =>
I have three models: class Address < ActiveRecord::Base has_many :jobs end class Category <
I have three models: class A < ActiveRecord::Base has_many :bs end class B <
Three models: class Customer < ActiveRecord::Base has_many :visits end class Visit < ActiveRecord::Base belongs_to
I have three models : class LevelTwoArea < ActiveRecord::Base has_many :places, dependent: :restrict end
I have three models defined as follows: class Comic < ActiveRecord::Base has_many :feeds end
I have three models: class Client < ActiveRecord::Base has_many :balancesheets has_many :investment_assets, :through =>
I have three models: class User < ActiveRecord::Base has_many :projects, :through => :permissions class
I have three models class Collection < ActiveRecord::Base has_many :presentations has_many :galleries, :through =>

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.