77426

Rails: Multiple databases, same schema

Question:

I'm in the middle of a fictional scenario project where I have allowed multiple users for a company to log in, create records, and so on, who all connect to the one database. They can all records absence records, attendance records, and so on.

What I want to do however, is use this same schema but expands this to allow several <strong>companies</strong> to have their own databases using the same schema. So each company will have their own data, but all companies use the same data model. In other words all company's can create absence records, but they each only have access to their own absence records that they created themselves.

How can I achieve this?

All I need is two or three files for this, I'm not going commercial with it in case you guys think I'm cutting corners at someone else's expense!

Something as simple as an if-else that decides which file to use would be very useful to me, so if such a line of code exists please let me know.

Answer1:

I think you are doing it wrong (unless you have a really good reason to have a database for each company), because it seems like you are repeating your data model over and over while introducing unnecessary complexity to your code.

Try to have all the companies in one DB/tables with having separated by the company_id.

Ex: data structure would be as follows

companies table id name users table id user_name company_id

However if you really want to connect to multiple databases, check this <a href="https://stackoverflow.com/questions/1825844/multiple-databases-in-rails" rel="nofollow">SO question</a>.

Recommend

  • Export descriptive statistics row of values into an Excel Sheet from R
  • DateTime String to Date, In Time and Out Time
  • String error in SQL MS Access
  • How to set up multiple aliased joins between two models?
  • Java Regex multiple groups match & extract [duplicate]
  • Jest - testing a component that uses react-router
  • Insane Chrome issue…Chrome renders twisties?
  • Resize of Iframe dynamically depending on the content of the Iframe of the Iframe's src URL
  • Passing/Returning references to object + changing object is not working
  • Using vba to copy the contents of a word document into another word document
  • updating and compacting sqlite database in android
  • Is it good to have multiple database running in a same project?
  • Conditionally encapsulate rules
  • parameterized queries in oursql
  • MySQL: Difference between `… ADD INDEX(a); … ADD INDEX(b);` and `… ADD INDEX(a,b);`?
  • Nested projects in multiproject visual studio templates
  • EntLib Way to Bind “Null” Value to Parameter
  • Retaining data after updating application
  • Examples of how to a STS in .Net 4.5 using WCF
  • Rest Services conventions
  • Django model inheritance, filtering models
  • ActiveRecord query for a count of new users by day
  • The plugin 'org.apache.maven.plugins:maven-jboss-as-plugin' does not exist or no valid ver
  • swift auto completion not working in Xcode6-Beta
  • Is possible to count alias result on mysql
  • Does CUDA 5 support STL or THRUST inside the device code?
  • htaccess rewriting URLs with multiple forward slashes
  • SVN: Merging two branches together
  • jqPlot EnhancedLegendRenderer plugin does not toggle series for Pie charts
  • How do I rollback to a specific git commit
  • Is there a mandatory requirement to switch app.yaml?
  • Benchmarking RAM performance - UWP and C#
  • embed rChart in Markdown
  • MATLAB: Piecewise function in curve fitting toolbox using fittype
  • Busy indicator not showing up in wpf window [duplicate]
  • How to get NHibernate ISession to cache entity not retrieved by primary key
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • Binding checkboxes to object values in AngularJs
  • How can I use `wmic` in a Windows PE script?
  • Unable to use reactive element in my shiny app