Topic: User to Company logic based on domain name

Hi all,

I'm working on a little web app and need some help.

After a user signs up, they are assigned to a Company based on their domain name if it exists otherwise it will create a new Company with that domain.

def assign_user_to_company
 domain = email.split("@").last
 user_company = Company.find_or_create_by_domain domain
 update_attribute(:company_id, user_company.id) #or whatever you called this field
end

This is great because it groups all users into a network via their email. Yammer style. However I'm going to be listing all of these Companies and the Users within them. The problem arises when a few people sign up with google.com for example, and people in the same company register with a google.co.uk domain. This then creates separate groups of the same company.

I've been trying to think of a few ways to do this. Assigning Companies to a Group, giving a Company a parent...etc but not sure of the most efficient way of solving this problem.

Any help is much appreciated.