Skip to content

Instantly share code, notes, and snippets.

# Default Output
2.3.1 :004 > e.start_time.to_json
=> "\"2016-07-11T00:00:00.000Z\""
2.3.1 :010 > e.start_time.to_time.to_json
=> "\"2016-07-11T00:00:00.000+00:00\""
# Expected Output
2.3.1 :009 > e.start_time.strftime("%Y-%m-%dT%H:%M:%S").to_json
class Event < ActiveRecord::Base
def as_json(options = {})
super(only: [
:id, :name, :seats, :price, :images_data, :status,
:lat, :long, :address, :city, :zip
]
).tap do |h|
Category.all.each do |c|
h[parameterize(c.name, separator: '_').to_sym] = dishes.type_of(c.name).first
end
@product = # Some product
@customer = # some customer
@wishlist = @customer.wishlist.present? ? @customer.wishlist : @customer.create_wishlist
if @product
@product_included = @wishlist.products.include?(@product)
unless @product_included
@wishlist.products.push(@product)
@wishlist.save
end
end
class User < ActiveRecord::Base
has_many :events, foreign_key: :chef_id, dependent: :destroy
has_many :reservations, foreign_key: :guest_id
has_many :guest_events, -> { merge(Reservation.accepted) },
through: :reservations, source: :event
has_many :all_guest_events, through: :reservations, source: :event
has_many :event_notifications, -> { uniq }, through: :all_guest_events,
source: :notifications
has_many :notifications
end
[1] pry(main)> Date.today
Sun, 31 Jul 2016
[2] pry(main)> Date.yesterday
Fri, 29 Jul 2016
[3] pry(main)>
class User < ActiveRecord::Base
has_many :events, foreign_key: :chef_id, dependent: :destroy
has_many :reservations, foreign_key: :guest_id
has_many :guest_events, -> { merge(Reservation.accepted) },
through: :reservations, source: :event
enum role: [:chef, :guest, :both]
end
class Event < ActiveRecord::Base
has_many :reservations, dependent: :destroy
@alfie-max
alfie-max / Count Code lines
Created August 4, 2016 10:30 — forked from amitchhajer/Count Code lines
Count number of code lines in git repository per user
git ls-files -z | xargs -0n1 git blame -w | perl -n -e '/^.*\((.*?)\s*[\d]{4}/; print $1,"\n"' | sort -f | uniq -c | sort -n
@alfie-max
alfie-max / gist:9369511921deedddb31b634878d94a19
Created August 24, 2016 11:20 — forked from cpjolicoeur/gist:3590737
Ordering a query result set by an arbitrary list in PostgreSQL

I'm hunting for the best solution on how to handle keeping large sets of DB records "sorted" in a performant manner.

Problem Description

Most of us have work on projects at some point where we have needed to have ordered lists of objects. Whether it be a to-do list sorted by priority, or a list of documents that a user can sort in whatever order they want.

A traditional approach for this on a Rails project is to use something like the acts_as_list gem, or something similar. These systems typically add some sort of "postion" or "sort order" column to each record, which is then used when querying out the records in a traditional order by position SQL query.

This approach seems to work fine for smaller datasets, but can be hard to manage on large data sets with hundreds (or thousands) of records needing to be sorted. Changing the sort position of even a single object will require updating every single record in the database that is in the same sort group. This requires potentially thousands of wri

@alfie-max
alfie-max / gist:5c28c8f282064b653911a9ab8ea25c81
Created September 22, 2016 09:24 — forked from 1v/gist:04901ed17202cddfe42d
God and Puma configuration

God and Puma configuration

Add to Gemfile:

gem 'puma'
gem 'god'

Run in app folder:

bundle install
class Hash
def compact_me!
delete_if do |k,v|
if v.is_a? Hash
v.compact_me!
false
else
!v.in?([true, false]) && v.blank?
end
end