skip to Main Content

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.

2

Answers


  1. Chosen as BEST ANSWER

    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');')
    

  2. It would be more usual to save configuration info as heroku config vars, rather than in Postgres.

    heroku config will list the current vars and heroku config:set VAR_NAME=var_value will set vars. This is not Ruby code – it is a part of heroku setup.

    If you truly want to write Ruby on Rails code to modify the database, you need to learn ActiveRecord

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search