Search code examples
ruby-on-railsherokurubygemsheroku-postgresshopify-app

How to access heroku postgres Database with ruby on rails in shopify app


Hello I am working on a shopify embedded app with ruby on rails and I must save some config parameters given by the user or me. I couldn't find any solution other than using heroku postgres database.

I looked everywhere for a code to insert or select data from the heroku database but I was unsuccessful in the Doc they literally gave one line of code in Ruby section.

https://devcenter.heroku.com/articles/heroku-postgresql#connecting-in-ruby

in the Gemfile add :

gem 'pg'

I have no idea how to execute any Query on the database with ruby in my controller.

EDIT : I did all the configuration for the pg gem all I need to know is some example of code on how to use it

Use case example:

  1. The app home have Input and Save button.
  2. User 1 Enter data in input and Click save.
  3. User 1 close the app.
  4. User 1 returns to the app.
  5. User 1 finds the data he wrote in the input.

Please help.


Solution

  • Solved It with the help of reddit community.

    All I had to do is adding this in my controller:

    @mydb = PG.connect(ENV['DATABASE_URL'])
    

    Query Example

    @mydb.exec('insert into table values (\'test2\',\'test2\',\'2\');')