How to write to Postgresql using activerecord-sinatra. I installed PostgreSQL after
1 - install gem pg - gem install pg
2 - installed gems: gem install activerecord gem install sinatra-activerecord gem install rake # to apply migrations
3 - created a connection to the database in the app.rb file
# app.rb configure :development do set :database, {adapter: "postgresql", encoding: "unicode", database: "your_database_name", pool: 2, username: "your_username", password: "your_password"} end configure :production do set :database, {adapter: "postgresql", encoding: "unicode", database: "your_database_name", pool: 2, username: "your_username", password: "your_password"} end 4 - Created a model in the app.rb file
class Article < ActiveRecord::Base end 5 - Set migration in rakefile file
require 'sinatra/activerecord' require 'sinatra/activerecord/rake' require './app' 6 - Then created a migration command in the console
rake db:create_migration NAME=create_articles 7 - In the newly created migration file
class CreateArticles < ActiveRecord::Migration def change create_table :articles do |t| t.string :title t.string :content t.boolean :published, :default => false t.datetime :published_on, :required => false t.integer :likes, :default => 0 t.timestamps null: false end end end 8 - Run commands in rake db: create and rake db: migration console
The database has been created, the psql console is displayed in the console, and how can I now write to it in sinatra?