I have a rails app that should access some other databases for reading only. These Databases are created and used by other applications and I only need the to read.
Its not a problem to connect to these databases but I dont want to create a model for each table in these databases manually because there are some hundred tables. Is there a best practice for accessing a foreign database with rails?
Yes we can access foriegn databases in Rails application
Checkout this link
http://www.paultastic.com/showpage/Grabbing-Data-From-Foreign-Database-Using-Rails
Related
I need to create a new rails application that will use an existing external Microsoft SQL server database as its primary database. This DB is running on an external server that i'll need to connect to. Here where i work there's a separate DB Admins section so im not sure what steps i need to perform. I think the easy part is connect to the server and set the app to work with that DB. But i get confused after that.
Should i create a db dump and migration?
Should i add models to represent existing database structure?
In case i need to create new models and tables in the db...
Should i generate its models from the rails generator and run migrations on the db or contact the DB admins to create the tables, and then specify manually the models inside the app? <- this really confuses me
Thank you in advance and sorry for my english! :)
EDIT!! -> The Rails Way
I've found another great way to integrate multiple database data... You can define a model and specify in which database it's located (so db connection is never closed)... This is definitely The Rails Way and should be followed...
Here's a nice tut:
Multiple Databases in Rails
TL;DR
Define your secondary_db in database.yml
Define your model and specify its database and table name
Class ExternalDatabaseModel < ActiveRecord::Base
establish_connection(:secondary_db)
self.table_name = "the_table"
self.primary_key = 'someWeirdIDColumn'
end
Then use as any other rails model, specify relationships and more :)
Hope it helps to somebody!
I would suggest you take a look at tiny tds gem: https://github.com/rails-sqlserver/tiny_tds. It's really use to config and use... you can use it to connect on your external sql server db and make the queries you want. Don't know which type of system you're working, but if you're constantly retrieving data, making external requests may get your system slower. Depending on your application, you could consider synchronizing the data, making a local database redundant. Hope it helps you, good luck!
EDIT: about the models part: if you're going to retrieve the data from that sql server every time you need, than you don't need to store it locally. But if you're doing that redundancy, you should create the models with the attributes you'll need, than when you retrieve from the server, you save it normally, just attributing the values to your model and calling save! method.
I have a rails application requirement which I have User table in main database and need to create UserJobs related records(sub tables) with in a sub database , i.e I need to create a different database for each user and store their Jobs in a different database.
How can I do this manually from rails application.
Apartment gem could come in handy. It lets you create multiple schemas in database with the ability of supporting multi-tenancy.
Apartment Gem would do all the things for you which you want to do as mentioned in your query.
I want to run 2 ruby on rails apps which share users, creating one app to do it all is not an option unfortunately. So I want to have single sign on, and user data shared across them. I'm unsure as to what the best solution to this would be.
Should I run them both off the same database?
Have 2 databases with both apps using the users table in one database only?
Some other solution?
You would have to have a dedicated development rails environment to manage migrations. And have the two "apps" pointing at this database. The downside is just generating models and relationships twice for those two apps is a manual task.
A cleaner solution is to have one rails environment with migrations, models and relationships. But build an API endpoint into this environment that those two apps can call in order to use the shared database.
How to re-use same database with different migration scripts for different rails apps. The following is the proposed structure, is this something which would work and is this a proper way to share the database?
rails-app1/db/migrate/s1.sql
rails-app1/db/migrate/s4.sql
rails-app2/db/migrate/s2.sql
rails-app2/db/migrate/s3.sql
rails-app2/db/migrate/s5.sql
I am using MySQL database
Maybe you should consider to create an API which will be the only one with rights to read/write in your database. Then, App1 and App2 will be consumers of this API.
What you are trying to do will create different database schemes with different (or duplicated) model definitions for the same Database... It sounds dangerous.
A friend and I have an idea to create multiple apps via heroku using Postgres.
Is there away we can tie in all the databases into one singular database by either pushing each one individually or just having them all indexed?
Yes use heroku config in the app with the provisioned database to get the connection string. Then use heroku config:set PG_URL=3829w:8eh8fe78hfd#ffeiwfafdsa.amazonaws.com/8fdHUfdsa to set the database in the other app. Now both apps will use the same database. They won't overwrite each other, but they will write to the same DB.
You can create multiple schemas, which will namespace the tables per-schema and avoid any name conflicts between each app's models.
https://devcenter.heroku.com/articles/heroku-postgresql#multiple-schemas
The most common use case for using multiple schemas in a database is building a software-as-a-service application wherein each customer has their own schema.
The flipside (as the link points out) is that multiple schemas can significantly reduce performance. You're also sharing connection limits, cache and disk space between apps. This might not be a problem depending on your usage, but just be aware of it.
Further reading:
CREATE SCHEMA docs: http://www.postgresql.org/docs/9.3/static/sql-createschema.html
Set schema_search_path in database.yml in each app to your individual schemas: http://api.rubyonrails.org/classes/ActiveRecord/ConnectionAdapters/PostgreSQLAdapter.html