Adding parent record to all tables

I want to add an "Organisations" table to a Rails app so that I can separate all tables by organisation.

I have put 'organisation_id' on all tables but I need to work out the best way to refactor the app to cope with the new relationships.

Any suggestions welcome especially if somebody has done something similar.

Cheers George

I think that a better way would be to create a separate database per organization. Every organization could have their own subdomain. And you could change the database connection during each request to the proper database. Another way would be to extend every table primary key with :organization_id. Of course to do that you would have to use plugin that adds composite primary keys support to active record.

Depending on the application code, if it is not too complicated and you always you AR queries (I mean, you do not genery by yourself sql queries) you could make a solution like this:

#application controller

before_filter :find_organization around_filter :within_organization

private

def find_organization   @organization = Organization.find(session[:organization_id]) end

def within_organization   ActiveRecord::Base.send(:with_scope, :find => where("organization_id = ?", @organization.id")) do     yield   end end

I am not sure whether it could works. Maybe you would have to do it with every class instead...

def within_organization   User.send(:with_scope, :find => where("organization_id = ?", @organization.id")) do     Article.send(:with_scope, :find => where("organization_id = ?", @organization.id")) do       Comment.send(:with_scope, :find => where("organization_id = ?", @organization.id")) do         yield       end     end   end end

Robert Pankowecki

Thanks for the ideas Rupert,

I have not really done much with filters so I might think about that. Not sure If I understand entirely what you are trying to do though.

The queries are not too much of a problem as I can inject an organisation_id into them and I have covered off the relationships by creating a super class for all the tables with a "belongs_to :organisation"

The bigger problem is to ensure that all the tables get the organisation_id set on create. I tried to set the organisation_id in an Activerecord callback in the superclass of all the models but I cant access the organisation id from within the model.....

George

I have fixed the problem of getting the organisation_id into all the records without hundreds of assignements in controllers...

I have an observer:

class BaseTableObserver < ActiveRecord::Observer     cattr_accessor :current_organisation_id

    def before_validation(base_table)         if base_table.organisation_id.blank? && ! @@current_organisation_id.blank? #only do it if its not already set           base_table.organisation_id=@@current_organisation_id        end      end end

And the class variable @@current_organisation_id is set in the ApllicationController in a before filter.

This works..

I assume that since Ruby is single threaded that it is safe to use the Class/Static variable?

Cheers George

PS Using with_scope in around_filters appears to be a bit of a controversial subject! As does accessing session type variables from models. But in this case I am happier with this solution than lots of separate assignments

In other words your problem was to set the organization in every created model without pain ?

Ruby is not singlethreaded. You can even make your Rails app using multiple Thread by calling setting threadsafe in Rails configuration. I think Rails is threadsafe since 2.2 version. Instead of class accessor you could use Thread.current[:organisation_id]

Robert Pankowecki