3

i'm a ruby new bie.I'm trying to create a simple API using rack that takes a json data and writes that to the postgres heroku database table. i created a database on postgres heroku, but didn't find out any option to create a table there. Can any one provide some pointers to some good tutorials which explain how to go about accomplishing this.

3
  • Why not connect to the database and send it a bit of CREATE TABLE SQL? Commented Jan 31, 2013 at 9:19
  • ok in dat case every time i need to check if a table already exists before CREATE TABLE SQL Commented Jan 31, 2013 at 9:31
  • 1
    Or have a set up rake task that creates your tables and run that when you publish your application. Commented Jan 31, 2013 at 16:26

1 Answer 1

5

If you have a dev or basic plan, you won't be able to connect directly to the DB and manually create the table, so you will need to write a migration script that will create the table for you.

Using an ORM like ActiveRecord is generally a good idea and it makes it very easy to automate all the SQL grunt work. Use the sinatra-activerecord (https://github.com/janko-m/sinatra-activerecord ) gem to get the rake helper tasks on your rack app. Just make sure that instead of using local sqlite3, you use ENV[DATABASE_URL] to point to your Heroku db.

Then run the local command rake db:create_migration NAME=json_data to make your migration file and then once you have created it, run the Heroku command heroku run bundle exec rake db:migrate to create all the tables and schemas you need.

Sign up to request clarification or add additional context in comments.

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.