Skip to content

Instantly share code, notes, and snippets.

config.sequel.after_connect = proc do
Sequel::Model.db.extension :pagination
Sequel::Model.db.extension :pg_hstore
Sequel::Model.plugin :active_model
Sequel::Model.plugin :validation_helpers
Sequel::Model.plugin :dirty
Sequel::Model.plugin :association_proxies
Sequel::Model.plugin :timestamps, update_on_create: true
end
Artist.eager_graph(:albums).all
Artist.eager(:albums).all
@mrbrdo
mrbrdo / gist:6987758
Last active September 28, 2016 00:23
# verbose, flexible version (specify which columns to join on)
Album.left_join(:artists, artist_id: :id)
# association "automatic" version (automatically determines which column to join on based on association on the model)
Album.association_left_join(:artists)
# AR and Sequel - results in single query, *not* two
Artist.where(id: Album.select(:artist_id))
# SELECT artists.* FROM artits WHERE id IN (SELECT artist_id FROM albums)
User.where { (id > 5) & (login_count < 10) }
User.where { |q| (q.id > 5) & (q.login_count < 10) }
# AR & Sequel
User.where(name: "Tom")
User.where("name = :name", name: "Tom")
User.where("name = ? OR email = ?", "Tom", "[email protected]")
User.where("users.name = ?", "Tom")
# AR-only
User.where(users: { name: "Tom" })
# Sequel-only
User.where(users__name: "Tom")
User.where("name = ?", "Tom").first
User.where(name: "Tom").first
2.0.0p247 :005 > pid = spawn("true")
=> 79236
2.0.0p247 :006 > Process.waitpid(79236)
=> 79236
2.0.0p247 :007 > Process.waitpid(79236)
Errno::ECHILD: No child processes
Unicorn worker 1 | Unicorn worker 2
CREATE comment |
post = Post.find(X) | DELETE post X
any code | Post.find(X).destroy
post.comments.create |
has_one :display_gallery_item, as: :imageable, class_name: "GalleryItem"
class ServiceCenterPreloader
class GalleryItemPreloader < ActiveRecord::Associations::Preloader::HasOne
def initialize(owners, reflection)
super(GalleryItem, owners, reflection, ActiveRecord::Relation.new(nil, nil))
end
private
def build_scope