Skip to content

Instantly share code, notes, and snippets.

View JuzerShakir's full-sized avatar
🎯
Focusing

Juzer Shakir JuzerShakir

🎯
Focusing
View GitHub Profile
@JuzerShakir
JuzerShakir / table_exists.sql
Created June 24, 2022 06:21
To check if a table already existed with that name, in either case, it will return a successful query:
CREATE TABLE IF NOT EXISTS countries (
);
@JuzerShakir
JuzerShakir / dup_table_without_data.sql
Created June 24, 2022 06:11
Create a duplicate table from an existing table without any data:
CREATE TABLE dup_countries
AS SELECT *
FROM countries
WITH NO DATA;
@JuzerShakir
JuzerShakir / dup_table_with_data.sql
Created June 24, 2022 06:06
Create a duplicate table from an existing table with data:
CREATE TABLE dup_countries
AS SELECT *
FROM countries;
@JuzerShakir
JuzerShakir / schema.rb
Created December 16, 2021 15:13
RSpec
class CreateSmartphones < ActiveRecord::Migration[6.1]
def change
create_table :smartphones do |t|
t.string :brand, null: false
t.string :model, null: false
t.timestamps
end
end
end
@JuzerShakir
JuzerShakir / new.html.erb
Created October 29, 2021 10:09
Used in medium
<%= form_with model: @user, url: users_create_path do | field | %>
Name
<%= field.text_field :name %>
<br>
<%= field.submit :Create %>
<% end %>
def create
@user = User.new(users_params)
if @user.save
redirect_to @user
else
render "new"
end
end
@JuzerShakir
JuzerShakir / users_controller.rb
Created October 29, 2021 04:08
used in medium
class UsersController < ApplicationController
def new
end
def create
end
def update
end
@JuzerShakir
JuzerShakir / users_controller.rb
Created October 29, 2021 04:06
Used in medium
class UsersController < ApplicationController
def new
end
def create
end
def update
end
end
@JuzerShakir
JuzerShakir / schema.rb
Created October 29, 2021 03:50
Used in medium controller article
create_table "passports", force: :cascade do |t|
t.integer "number"
t.integer "user_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_passports_on_user_id"
end
create_table "users", force: :cascade do |t|
t.string "name"
@JuzerShakir
JuzerShakir / validates_each.rb
Created October 23, 2021 18:50
Used in medium
validates_each :email do | record, attr, value |
# extracts email service
service = value.match(/\A\w+\.?\w*\@{1}([a-z]+)\.{1}[a-z]+\z/).to_a[1]
# creates error for the attribute
record.errors.add(attr, "#{service} email service is not allowed!") if service.in? %w(yahoo msn aol hotmail)
end