Topic: How to model Persons and their Organisations?

Hello,

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.

Re: How to model Persons and their Organisations?

Firstly, I think you'll find that the plural of person is people. Therefore

User can have many Persons

Should be

User has many people 

If you don't follow conventions you'll get into a twist.

Now if you introduce an organisation model then you can have

person belongs_to organisation
organisation has_many people

The person belongs_to organisation relationship is only enforced if you declare validations, so by simply introducing an organisation_id into the person table and declaring the belongs_to declaration you are NOT forcing a person to belong_to an organisation. You must obviously also take care to allow null values in the migration (, null: true)

Consider the following

org = Organisation.where(:name => "Acme") 
org.people

You will see only people that belong_to the "Acme" organisation

You could also set up a scope in the Person model to find private_people (Note private is a reserved term shence private_people rather than just provate) by searching for people where organisation is null or blank?

Because scopes are chainable this would also allow you to do user.people.private_people

Or even better you could define a user as having many private people by using :condtitions

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: How to model Persons and their Organisations?

Hello James,

Thanks for your kind answer.

What you describe is pretty much what I had in mind so far.

There are just two things I am still struggling with:

1. If a person does not belong to any organisation, where should I store that person's address data? Would you recommend to create another model Addresses for that purpose? It could contain the address data of all people and organisations. Will that reduce the amount of duplication in my Rails app or am I misguided here?

2. The view code for creating a new person is still a mystery to me. I guess I will need a checkbox labelled "Organisation" that users can tick when the person to be created does belong to an organisation. If that checkbox gets ticked, the address data has to be stored in a different way than it would for a private person. No idea how to do that yet...

Last edited by Tintin81 (2012-10-04 09:57:10)

Re: How to model Persons and their Organisations?

If a person does not belong to any organisation, where should I store that person's address data? Would you recommend to create another model Addresses for that purpose?

Yes, polymorphic relationships are what you need for an address.
Check this rails cast on polymorphic relationships and thenk in terms of an address belonging to either a person OR an organisation
http://railscasts.com/episodes/154-poly … ssociation
and the revised subscription only version
http://railscasts.com/episodes/154-poly … on-revised

I guess I will need a checkbox labelled "Organisation" that users can tick when the person to be created does belong to an organisation

No, use a drop down selection box to select an organisation that has a default value of null
Selection lists are described here http://api.rubyonrails.org/classes/Acti … elper.html
But there are many nice jquery and javascript alternatives
Here are a couple
http://railscasts.com/episodes/258-token-fields-revised
http://railscasts.com/episodes/102-auto … on-revised
http://railscasts.com/episodes/234-simple-form-revised

Those are subscription only casts but there are earlier free versions that will give you the idea and you can always check out the Rails docs for current implementation.

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)

Re: How to model Persons and their Organisations?

Hello James,

Those RailsCasts are great. I just signed up as a pro member, so I was able to watch the revised versions as well :-D

The ones you suggested were exactly the ones I needed.

I created an Organisation model and a polymorphic Address model and everything seems to fit together nicely.

Actually my plan was to have users create new organisations from within the people view as well. This would have been very convenient for the user. But according to what I've read today, this will not work with

accepts_nested_attributes_for

because one organisation has_many :people. Am I right here or is there another way to create a new organisation from within a people view?

The alternative would probably be much easier to realise for me, but also a bit more cumbersome for the user. There would just be a regular organisations_controller where users can CRUD organisations using a number of views.

What is the best way to go?

Last edited by Tintin81 (2012-10-05 11:17:57)

Re: How to model Persons and their Organisations?

Yeah, those casts have been free for years, Ryan Bates has just started the pro version and it's well worth the subscription.
It is possible to create a new organisation.

Add a label and a company name field that do not belong to the form object, that way the params will be organised appropriately in the controller when the form gets posted.
For the form use something like

<%= label_tag :new_organisation_name, "Organisation" %><br />
<%= text_field_tag :new_organisation_name %>

Then in your controller create action for a new person you could do something like this

#As normal
@person = Person.new(params[:person])
#Add a new organisation if needed
unless params[:new_organisation_name].empty?
  @organisation = Organisation.new
  @organisation.name = params[:new_organisation_name]
  @person.organisation = @organisation
end
@person.save # etc...

Basically the above will create a new organisation only if something (Anything at all) has been entered into the new organisation name field in your form and regardless of whether or not an existing organisation has been selcted, the new organisation will be assigned to the person.
The fact that the new organisation has been added to the person association means that it rtoo will be saved when the person has been saved, although I never feel very confortable with this and usually I add an explicit savbe to both models, that way I can handle validation errors seperately.

One of the most important things to learn about forms is how params are arranged and how you can manipulate the params hash in your controller. Use your log file to see what params are getting passed back to your actions and study the code to see how to make use of the params

What you want and what you need are too often not the same thing!
When your head is hurting from trying to solve a problem, stop standing on it. When you are the right way up you will see the problem differently and you just might find the solution.
(Quote by me 15th July 2009)