Designing multi tenant app in rails -
i'm implementing multi tenant app rails. approach not use postgres inbuilt multi tenant feature , add column record subdomain. question :)
let's example
class organisation < activerecord::base has_many :users end class user < activerecord::base belongs_to :organisation end
i'm thinking 2 approaches here,
approach 1
add subdomain
column organisations
- pros - way how relational databases should work \0/
- cons - when have more complex queries , make code slow
approach 2
add subdomain
column both organisations
, users
- pros - make queries faster
- cons - i'm going against relational databases
so question is, sort of method should follow between above two, or there different approach didnt think about
thanks in advance
we run multi-tenant rails app fewer 500 table-backed classes, , if had guess i'd around 400 of them relate client data.
client-specific attributes held in client
model, add client_id
every client table not null constraint on database. minority of them indexed, though because accessed through parent record.
we not have worry setting client id because model have:
class child after_initialize self.client ||= parent.client end end
we add client_id
many tables because have lot of code does:
@books = current_user.client.books
... in cases we'll have association directly client
model, , client_id
indexed.
we add client_id
tables, though, because often, operational or unusual reasons, want able find of relevant records client ...
marketingtext.where(client: client.snowbooks).group(:type).count
... , having go through parent record inconvenient.
also, because made decision on client-specific tables, not have make decision on each one.
so question, add subdomain organisation
only. however, add organisation_id
column every table holding organisation-specific data.
if have lot of clients going familiar subdomain, write meta-program method on organisation lets use:
organisation.some_subdomain
... required organisation, find child records (in table) association directly organisation model ...
organisation.some_subdomain.users organisation.some_subdomain.prices organisation.some_subdomain.whatevers
Comments
Post a Comment