0

I have a Rails app A with a postgres database. I also have another Rails app B with a postgres database. Now I want to reuse some of the data of app B in app A. What is the best way to import that data? I assume a rake task can be used for this, but how would you do this?

Do you need to add the connection details of the database of app B in the database.yml of app A? And how do I actually get the data?

3
  • do you want to keep them synced? or is it a time connection to just bulk import? Commented Jun 24, 2013 at 12:34
  • @Denis That's pretty much an answer as it stands; consider posting it as such. Commented Jun 24, 2013 at 12:43
  • It is for a one time bulk import of some data Commented Jun 24, 2013 at 13:07

2 Answers 2

1

You can do a manual connection via activerecord

require 'active_record' 
ActiveRecord::Base.establish_connection(
  :adapter => "mysql", 
  :host => "localhost", 
  :username => "root", 
  :password => "abcd", 
  :database => "funonrails")

or do something like this

dbconfig = YAML::load(File.open('database.yml')) 
ActiveRecord::Base.establish_connection( dbconfig[:students_development] )
Sign up to request clarification or add additional context in comments.

2 Comments

This looks interesting, but how would I actually make a request to this database, like "people = People.all"? How does it know it should get this from database B and not from database A?
You have to pass the database B address to the connection. Actually, I think you'll need to instantiate 2 activerecord connections, connectionA and connectionB. Take a look at this link: railskey.wordpress.com/2012/07/20/… it may help you
1

Depending on what you actually mean by import, you could use dblink to query the other DB directly:

http://postgresql.org/docs/current/static/dblink.html

When PostgreSQL 9.3 gets released, be sure to also look into the new foreign data wrapper:

http://postgresql.org/docs/9.3/static/postgres-fdw.html

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.