Search code examples
ruby-on-railsjquery-mobileherokumobile-application

How do i work with one database in two different rails apps


I want to create a mobile version of my rails app so I made a new rails app and wanted to build it with phonegap and jquery-mobile. the thing is that i want to use it in a way that the mobile app id just connecting with the app already existing on heroku servers.

Is it possible?


Solution

  • Yes its possible! Heroku stores all of your data on Amazon AWS and you can access these databases. Note, I've only done this manually using a PG connections to the database and not with ActiveRecord.

    This is what I recommend, create a RESTful API on your rails app that your mobile app can call for reads, inserts, deletes and updates. And then program your Rails App to do the following:

    I'm going to assume you are using PostGreSQL such as the majority of Heroku Apps. Do the following: 1. Go to https://postgres.heroku.com/login and login 2. Choose the Database you would like to access 3. Copy down the details under the "Connection Settings Tab"

    Now in your app, create some instance variable for the various connection settings like so:

    @dbname = "your db name"
    @host = "your host"
    @user= "your user details"
    @password = "your password"
    @port = "5432"
    @sslmode = "require"
    

    Then create a new connection to the database via:

    conn = PG::Connection.new(:dbname => @dbname, :host => @host, :user => @user, :password => @password, :port => @port, :sslmode => @sslmode)
    

    You can use this connection for reads, inserts, updates, deletes from both your apps. For more information on how to do that go to: http://deveiate.org/code/pg/PG/Connection.html

    Hope this helps!