# Adding parent record to all tables

**URL:** <https://www.ruby-forum.com/t/adding-parent-record-to-all-tables/195175>\
**Category:** Rails\
**Created:** [September 18, 2010, 8:15am UTC](https://www.ruby-forum.com/t/adding-parent-record-to-all-tables/195175 "2010-09-18T08:15:42Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [September 18, 2010, 8:15am UTC](https://www.ruby-forum.com/t/adding-parent-record-to-all-tables/195175/1 "2010-09-18T08:15:42Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [September 18, 2010, 12:36pm UTC](https://www.ruby-forum.com/t/adding-parent-record-to-all-tables/195175/2 "2010-09-18T12:36:43Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [September 18, 2010, 11:08am UTC](https://www.ruby-forum.com/t/adding-parent-record-to-all-tables/195175/3 "2010-09-18T11:08:20Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [September 18, 2010, 2:40pm UTC](https://www.ruby-forum.com/t/adding-parent-record-to-all-tables/195175/4 "2010-09-18T14:40:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![giorgio](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/g/cab0a1/32.png) [@giorgio](https://www.ruby-forum.com/u/giorgio)\
**Post date:** [September 19, 2010, 10:16am UTC](https://www.ruby-forum.com/t/adding-parent-record-to-all-tables/195175/5 "2010-09-19T10:16:00Z")

</div>

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
