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 6101237
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T13:28:38+00:00 2026-05-23T13:28:38+00:00

sorry if a similar question has been posted before, i didn’t know what terms

  • 0

sorry if a similar question has been posted before, i didn’t know what terms to use to search for it.

i have a model ‘vehicle_workshop’, it stores both vehicles and workshop data, there are is column called ‘type’ to distinguish between the two, ‘workshop_no’ column serves as a name

now i have another model ‘workshop distributions’, it uses both vehicles and workshops, i am able to create a relationship as follows

belongs_to :vehicle_workshop
belongs_to :vehicle_workshop, :foreign_key => "vehicle_id"

this allows me to save the data, with no issues, the two forign key fields are ‘vehicle_workshop_id’ for workshops and ‘vehicle_id’ for vehicles.

now the problems is when i want to access the registration no, i.e the field ‘workshop_no’ for both vehicles and workshops.

i have 2 attr_accessor’s , ‘vehicle_no’ , ‘workshop_no’

the methods are as follows

def vehicle_no
    self.vehicle_workshop ? self.vehicle_workshop.workshop_no : nil
end

def workshop_no
    self.vehicle_workshop ? self.vehicle_workshop.workshop_no : nil
end

both these functions returns the same value, how will i pass the appropriate id values to both these functions? say ‘vehicle_id’ and ‘vehicle_workshop_id’ ?

any help would be greatly appreciated.

  • 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-05-23T13:28:38+00:00Added an answer on May 23, 2026 at 1:28 pm
    class WorkshopDistribution
      # You could change your references to these:
      belongs_to :vehicle_workshop
      belongs_to :vehicle, :foreign_key => "vehicle_id", :class_name => 'VehicleWorkshop'
    
      # and then just change methods:
      def vehicle_no
        vehicle ? vehicle.workshop_no : nil
      end 
    
      def workshop_no
        vehicle_workshop ? vehicle_workshop.workshop_no : nil
      end
    end
    

    but I would suggest you to use STI(Single Table Inheritance) for such situation, if you have a possibility to change DB structure.

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

Sidebar

Related Questions

Sorry I know similar question have been asked many times before but like most
Sorry if this has been asked before - there's a question with a similar
So, if this question has been asked before, I'm sorry. I'm not exactly sure
I'm sorry, I'm sure a similar question has already been asked but I'm afraid
Sorry if this has been answered before, but all the related questions didn't quite
Sorry if this question has been answered before; however all of the questions that
Sorry, if this has been asked and answered here, simple search didn't give me
Sorry if this question has all ready been asked, maybe someone can point me
I know this question has been asked a couple of times, but there's something
Sorry to post another question that is similar to the question that I posted

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.