Skip to content

Instantly share code, notes, and snippets.

class AddRoleToUsers < ActiveRecord::Migration[7.2]
def up
execute <<-SQL
DO $$
BEGIN
IF NOT EXISTS (SELECT 1 FROM pg_type WHERE typname = 'user_role') THEN
CREATE TYPE user_role AS ENUM ('user', 'admin');
END IF;
END
$$;
@ekuzovlev
ekuzovlev / polymorphic_many_to_many_in_rails.md
Created February 6, 2024 21:13 — forked from bf4/polymorphic_many_to_many_in_rails.md
a polymorphic many-to-many association in Rails

This was my solution for a polymorphic many-to-many association

class ItemCountry < ActiveRecord::Base
  belongs_to :locatable, :polymorphic => true
  belongs_to :country
  # fields are :locatable_id, :locatable_type, :country_id
end

class Title < ActiveRecord::Base

has_many :countries, :through => :item_countries, :as => :locatable

How to deploy a Rails 7.1 app with Postgres and Kamal on a single server

I think you have looked at the tutorial from Mr. Heinemeier Hansson at least once or twice and have a similar setup.

rails new kamal_pg --css tailwind --skip-test --database=postgresql

cd kamal_pg
@ekuzovlev
ekuzovlev / README.md
Created January 19, 2024 06:07 — forked from jesster2k10/README.md
JWT Auth + Refresh Tokens in Rails

JWT Auth + Refresh Tokens in Rails

This is just some code I recently used in my development application in order to add token-based authentication for my api-only rails app. The api-client was to be consumed by a mobile application, so I needed an authentication solution that would keep the user logged in indefinetly and the only way to do this was either using refresh tokens or sliding sessions.

I also needed a way to both blacklist and whitelist tokens based on a unique identifier (jti)

Before trying it out DIY, I considered using:

@ekuzovlev
ekuzovlev / byebug_commands.md
Created November 11, 2022 08:24 — forked from elrayle/byebug_commands.md
Byebug Cheatsheet - organized by related commands

Byebug Cheatsheet

This cheatsheet includes most of the byebug commands organized by related commands (e.g. breakpoint related commands are together).

To see official help...

Command Aliases Example Comments
help h h list top level of all commands
help cmd h cmd-alias h n list the details of a command (example shows requesting details for the next command) (this works for all commands)
# controller.rb
# parsing errors from response
errors = Nokogiri::XML(response.body).css('Error').map(&:text)
# add errors to model.errors
errors.each do |err|
@test.errors.add :base, :invalid, message: err
end
# partials/_error.html.erb
From https://github.com/philsmy/cable-guy-example/blob/1ad67e6e929a9f4a4d6357c5283a36aef8c6debc/SetUpServer.txt
# set up user
ssh root@yourserverip
adduser deploy
adduser deploy sudo
vi /etc/ssh/sshd_config
enable PasswordAuthentication
save
systemctl restart sshd
exit

Перезагрузка приложения

passenger-config restart-app

Перезагрузка Nginx

service nginx restart
@ekuzovlev
ekuzovlev / Github.md
Last active May 26, 2021 07:06
Частые команды

error: Your local changes to the following files would be overwritten by merge: Please commit your changes or stash them before you merge.

git checkout file/to/overwrite
git pull