Skip to content

Instantly share code, notes, and snippets.

View alessandrostein's full-sized avatar

Alessandro Stein alessandrostein

View GitHub Profile
@agungyuliaji
agungyuliaji / activerecord-find-by-year-day-or-month-on-a-date-field.rb
Last active September 12, 2023 00:13
ActiveRecord Find By Year, Day or Month on a Date field
Model.where(:date_column => date)
Model.where('extract(year from date_column) = ?', desired_year)
Model.where('extract(month from date_column) = ?', desired_month)
Model.where('extract(day from date_column) = ?', desired_day_of_month)
@brodock
brodock / logrotate_rails_application
Last active August 29, 2015 14:04
Silver Bullet Nginx Vhost Recipe for Rails application with HTTPS + Webp + Capistrano + Linux FHS
# This file should be placed at /etc/logrotate.d/
/srv/*/shared/log/*.log {
daily
missingok
dateext
rotate 30 # amount of days to keep compressed logs (you should backup it up externally)
compress
delaycompress
notifempty
@dimroc
dimroc / eager_pagination.rb
Last active December 28, 2020 21:36
How to eager load AR associations when using Elasticsearch-rails and Kaminari
class EagerPagination < SimpleDelegator
attr_reader :records, :scope
def initialize(records, scope)
super(records)
@records = records
@scope = scope
end
def each
@checco
checco / rw_ro_access.sql
Last active March 20, 2025 09:36 — forked from oinopion/read-access.sql
How to create a read only user in AWS RDS PostgreSQL and a user with superuser privileges on AWS RDS PostgreSQL
--
-- Read only
--
-- Create a group
CREATE ROLE postgres_ro_group;
-- Grant access to existing tables
GRANT USAGE ON SCHEMA public TO postgres_ro_group;
GRANT SELECT ON ALL TABLES IN SCHEMA public TO postgres_ro_group;
module Archivable
extend ActiveSupport::Concern
included do
scope :archived, ->{ unscope(where: :archived_at).where.not(archived_at: nil) }
scope :without_archived, ->{ where(archived_at: nil) }
scope :with_archived, ->{ unscope(where: :archived_at) }
default_scope { without_archived }
end
# frozen_string_literal: true
# bundle exec rails runner create_accounts.rb
# An organization that the above users don't have access to
organization = Organization.find_or_create_by(slug: 'accounting-demo') do |o|
o.name = 'Accounting Demo'
o.invite_code = Rails.configuration.mx_iso_agent['invite_code']
end