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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T20:05:47+00:00 2026-05-12T20:05:47+00:00

Apologies if this is a slightly noob question, but looking to clarify my thoughts

  • 0

Apologies if this is a slightly noob question, but looking to clarify my thoughts on this. I have a model that can EITHER belong to one model, or another. For example:

Let’s say I have a Team model and I have a Member model, and both of those models can have one BankAccount.

class Team
  has_many :members
  has_one :bank_account
end

class Member
  belongs_to :team
  has_one :bank_account
end

class BankAccount
  belongs_to :team, :member
end

To me, the above makes sense, but I’d love to clarify this with some more experienced Rails people? Does Rails have any way of working out what the parent model is of any given BankAccount, baring in mind it could be one of two models? For example, if I called @bank_account.member on a Team bank account, will it throw a wobbly?

Thanks for your help.

  • 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-12T20:05:47+00:00Added an answer on May 12, 2026 at 8:05 pm

    You could use a polymorphic relationship.

    Your bank account would have the polymorphic relation.

    class BankAccount
        belongs_to :people, :polymorphic => true
    end
    

    And your two (or more) other models would have a simple has_many relation.

    class Member
        has_many :bank_accounts, :as => :people
    end
    

    In your bank account you can then use @account.people which will give you either a Member or Team object, depending of what it is.

    And in your Member or Team model, you can get the appropriate bank account with @member.bank_accounts.

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

Sidebar

Related Questions

Apologies if this was already asked but, I have had a look and can't
Apologies if this question has been asked already, but suppose we have this code
Apologies if this is a dumb question but I'm trying to format a currency
Apologies if this is a simple question, but I turn to the wisdom of
Apologies if this question has already been asked but I do not think I
Apologies if this is a too basic question but I couldn't find any satisfactory
Apologies if this question has been asked. I couldn't find it, but if it
Apologies if this has been answered before but I can't find a reference. I
Apologies if this question is a bit daft, but are there any negative repercussions
Apologies if this ends up being a stupid question, but I was just wondering

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.