As part of a learning exercise, although I would like this to become a
real application in the long run, I am trying to set up an accounting
application, for this application I need the users to be able to create
and use different Companies (Sets of books).
For maintenance reasons I would rather keep the data for each company
seperate in some way i.e. seperate tables with prefixes or a seperate
database. Having read up a bit on using multiple database connections I
think I would rather stick with one database and prefix the table
There are some ‘application wide’ tables for which I don’t want the
prefix one of which is the table that holds the different companies
that have been added. I already have code which creates the set of
prefixed tables from a set of template tables by using execute(sql) I
hope to improve this later by using schema/migration but for now
executing the raw sql statements is working.
My best guess after trying a few things is to use a before_filter in
the application.rb and my simple tests with one table prove that this
could work. (I am using a session variable to define which ‘company’ is
the one currently used). I suppose I could set up a hash/array to
define which models the table names need to be set for but I would
prefer this to a bit more dynamic i.e. not relying on me to remember to
put the model/table in my array/hash in application.rb.
I would rather have this prefixing in the model but I can’t figure out
how to get at the session variable for set_table_name in the model(s).
Any suggestions are welcome, I know I could just use tables with a
company column and filter all my finds appropiately but this would lead
to huge, more difficult to maintain, transaction tables in the long run.