Skip to content

Instantly share code, notes, and snippets.

@simi
Last active March 13, 2019 09:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save simi/3d1139dfe3dd8abf8800b253e52e250f to your computer and use it in GitHub Desktop.
Save simi/3d1139dfe3dd8abf8800b253e52e250f to your computer and use it in GitHub Desktop.
rails6 upsert example
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "rails", github: 'rails/rails', ref: '1816c4f'
gem "pg"
require "bundler/setup"
require "active_record"
require "logger"
ActiveRecord::Base.establish_connection(adapter: "postgresql", host: "localhost", database: "postgres", username: "postgres", password: "password")
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Schema.define do
create_table :posts, force: true do |t|
t.string :slug, null: false
t.text :post
t.timestamps
end
add_index :posts, :slug, unique: true
end
class Post < ActiveRecord::Base
validates :slug, uniqueness: true, presence: true
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment