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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T15:35:20+00:00 2026-05-30T15:35:20+00:00

I have been reading about Active Record Associations on RailGuides and I have found

  • 0

I have been reading about Active Record Associations on RailGuides and I have found it very informative but I definitely need help with understanding some of the choices. Still learning.

I have a Client model with the following attributes:

name
birth_date
address1
etc

Then a Contract model with these attributes:

auth_num
start_date
end_date
client_id .... I think I need this here for the new contract form

Also, a Code model with these attributes:

code_name
status
description

I have a join table ClientLines with these attributes:

Contract_id
Client_id

And a join table CodeLines with these attributes:

Contract_id
Code_id
Client_id
Units_Alloc   ... each contract might use a combination of codes some of which are 
                  the same in other contracts but they have different number of units 
                  allocated.

I am tempted to use the Polymorphic association because the Contract model is associated with the Client Model and the Code Model, but I am not confident enough to go ahead and set this up without first checking if someone is willing to give me some guidance on the examples I have listed?

My hope for guidance rests around these questions.

Is the polymorphic association the best choice for the example of models I have listed above?

Since Contracts use different combinations of Codes, but some of the Codes are the same in other Contracts with the only difference being the number of units allocated, do I have units allocated in the correct table? (Essentially I do not know where to put units alloc?)

When I set up the data-entry form such that I can enter in new contracts which will pull in specific attributes from the client table and code table, is it appropriate to have Client_id listed as one of the attributes of the Contract model and of course the units alloc attribute still looms large in my mind as a problem, where do I pull it from?

Any help or pointers would be most helpful.

Thanks.

  • 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-30T15:35:21+00:00Added an answer on May 30, 2026 at 3:35 pm

    Well, I’ll give it a try 🙂

    I understand, that we come from a Contract, that is a signed by one client and is about some “Codes”. So it can go like this:

    class Contract < ActiveRecord::Base
    has_one :client
    has_many :codes
    end
    
    class Client < ActiveRecord::Base
    belongs_to :contract
    end
    
    class Code < ActiveRecord::Base
    belongs_to :contract
    end
    

    Where you store contract_id in clients table and in codes table. Then you can do AR operations like:

    contract = Contact.find_by_auth_num(1234)
    contract.client #gets you the client
    contract.codes #gets you contracted codes
    contract.codes.count #gets you number of contracted codes
    

    Or if you want it more sophisticated you can change the relation to

    class Contract < ActiveRecord::Base
    has_one :client
    has_many :contract_codes
    has_many :codes, :through => :contract_codes
    end
    
    class Code < ActiveRecord::Base
    has_many :contract_codes
    has_many :contracts, :through => :contract_codes
    end
    

    with intermediate model of

    ContractCode < ActiveRecord::Base
    belongs_to :code
    belongs_to :contract
    end
    

    where you can store number of codes in a special column. Of course, all these declarations has to be backed up by proper migrations 🙂 Did I clear things a bit?

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

Sidebar

Related Questions

I have been reading about Quicksort and found that sometimes it' s referred to
I have been reading about exception handling on the Apple developer docs , but
I have been reading about dependency properties in several books but all have one
I have been reading about the syncroot element but I can't find it in
I have been reading about branch prediction but the only implementation I find are
I have been reading about try/finally on MSDN and found out following code. They
I need to discuss one thing with you. I have been reading about the
I have been reading about UnitOfWork pattern for last two days but didn't understand
I have been reading about this but I have to do some thing. I
i have been reading about proximity security devices through bluetooth, but i am 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.