I am currently building a Rails app where a User can have many Persons which in turn can have many Projects.
This works very well.
Now my problem is that some persons may belong to the same organisation while other persons may not belong to any organisation at all.
From what I’ve learnt so far, it would be good to move all the information pertaining the organisation into a separate table / model. (Please correct me, if I am wrong.) But what will happen then if a person does not belong to any organisation, i.e. is a private person?
How can all this be modelled in Rails?
Thanks for any help.
Is your problem simply like, You have persons that may or may not belongs to an Organization. Now how will you set it up?
Now as you said you will move the organization related data in a separate table say
organizations. And yourpersons(orpeople?) should contain aorganization_id. If the person doesnt belong to any organization, then it wil be simply null. The model relationships will be somewhat like.I skipped the relations with
UserandProjectmodel here. Please let me know if I missed any part of your question.Update: Based on your last comment
Lets list your scenarios:
If
addressis the only thing repeating in person and organization, then better move it in a separate tableaddresses.Organization:
has_one :addressPerson:
has_one :addressUpdate 2:
Since things are a bit conditional like
person.addresswill return the address of organization.person.addresswill return its own address.Then I would prefer the following way to keep it simple. Add a
current_address(or any preferred name) method in yourPersonmodel.call
person.current_addressinstead ofperson.addressto go through the decisions.