Skip to content

Instantly share code, notes, and snippets.

@sushant12
Created June 3, 2017 06:13
Show Gist options
  • Save sushant12/706a9f1d681d14bb674e7655dc008c93 to your computer and use it in GitHub Desktop.
Save sushant12/706a9f1d681d14bb674e7655dc008c93 to your computer and use it in GitHub Desktop.
class Task
DB = PG.connect :hostaddr => "127.0.0.1", :port => 5432, :dbname => 'testdb', :user => "postgres", :password => "postgres"
#uncomment while deploying to heroku
#DB = PG.connect ENV["HEROKU_POSTGRESQL_SILVER_URL"]
# uncomment to create pg database
# DB = PG.connect(hostaddr: "127.0.0.1", port: 5432, dbname: 'postgres', user: 'postgres', password: "postgres")
# DB.exec("CREATE DATABASE testdb")
# uncomment to create table
# DB.exec "DROP TABLE IF EXISTS tasks"
# DB.exec "CREATE TABLE tasks(Id SERIAL PRIMARY KEY, Name VARCHAR(20), Finished INT)"
class << self
def all()
DB.exec("select * from tasks")
end
def update(task, finish, id)
DB.exec("UPDATE tasks set name = '#{task}', finished = '#{finish}' where id='#{id}'")
end
def save(task_name)
DB.exec("INSERT INTO tasks (name, finished) VALUES ('#{task_name}',0)")
end
def delete(id)
DB.exec("delete from tasks where id='#{id}'")
end
def where(id)
DB.exec("select * from tasks where id = '#{id}'")
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment