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.
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:
Where you store contract_id in clients table and in codes table. Then you can do AR operations like:
Or if you want it more sophisticated you can change the relation to
with intermediate model of
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?