Skip to content

Instantly share code, notes, and snippets.

@yurko
Last active June 1, 2018 14:17
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 yurko/a2aaeecc8cf997ed7b2fb6e5a36244b7 to your computer and use it in GitHub Desktop.
Save yurko/a2aaeecc8cf997ed7b2fb6e5a36244b7 to your computer and use it in GitHub Desktop.
Rails chunks
# Self join
has_many :child_things, -> { order(:id) }, class_name: self.name
belongs_to :parent_thing, class_name: self.name, foreign_key: :thing_id
# LEFT OUTER JOIN with NULL
User.eager_load(:places).where(places: {id: nil})
# Unique index with condition
class CreateUniqueIndexForPayments < ActiveRecord::Migration
def change
add_index :payments, [:order_id, :amount, :payment_date, :status] name: 'unique_index_to_avoid_duplicate_payments', where: "status = 'Scheduled'", unique: true
end
end
# How to make one of "has_many :through" associations primary
# https://coderwall.com/p/tvn4za/how-to-make-one-of-has_many-through-associations-primary
class Place < ActiveRecord::Base
has_many :place_category_relations
has_many :categories, through: :place_category_relations
has_one :primary_category_relation, -> { where(is_primary: true) }, class_name: 'PlaceCategoryRelation'
has_one :primary_category, through: :primary_category_relation, source: :category
end
class PlaceCategoryRelation < ActiveRecord::Base
belongs_to :place
belongs_to :category
# Make sure only one category is primary per place
validates :is_primary, uniqueness: true, scope: :place_id
end
https://robots.thoughtbot.com/decorating-activerecord
class Profile < SimpleDelegator
# to make <%= form_for Profile.new(User.new) do |f| %><% end %> work
# it relies on profile.model_name.route_key => "users"
# ActiveModel::Naming defines model_name
extend ActiveModel::Naming
def to_partial_path
"profiles/profile"
end
# to make <%= render profile %> work
def to_model
self
end
end
group.admins << user
user.admin? # => true
user.editor? # => false
class Group < ActiveRecord::Base
has_many :memberships
has_many :users, :through => :memberships
has_many :admins, :through => :memberships, :source => :user,
:conditions => "memberships.role = 'admin'" do
def <<(admin)
proxy_association.owner.memberships.create(:role => 'admin', :user => admin)
end
end
has_many :editors, :through => :memberships, :source => :user,
:conditions => "memberships.role = 'editor'" do
def <<(editor)
proxy_association.owner.memberships.create(:role => 'editor', :user => editor)
end
end
end
class Membership < ActiveRecord::Base
belongs_to :group
belongs_to :user
end
class User < ActiveRecord::Base
has_many :memberships
has_many :groups, :through => :memberships
def admin?
memberships.where(:role => 'admin').first
end
def editor?
memberships.where(:role => 'editor').first
end
end
curl -I url -H 'Cache-Purge: 1'
Thing.where.not(image: nil).find_each do |e|
%i(large small).each do |v|
url = URI.parse(e.image.send(v).url)
req = Net::HTTP::Get.new(url.path)
req.add_field("Cache-Purge", "1")
res = Net::HTTP.new(url.host, url.port).start do |http|
http.request(req)
end
end
end
# add this into application.rb
initializer_file = Rails.root.join('config', 'initializers', 'my_initializer.rb')
reloader = ActiveSupport::FileUpdateChecker.new([initializer_file]) do
load initializer_file
end
ActiveSupport::Reloader.to_prepare do
reloader.execute
end
# Exclude new
things\/(?!new).*
Rails.application.routes.recognize_path '/your/path/here'
Rails.applicaion.eager_load!
ActiveRecord::Base.descendants
Sidekiq.redis {|c| c.del('stat:failed') }
puma -t 2:16 -p 3000
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment