Skip to content

Instantly share code, notes, and snippets.

View roryfranklin's full-sized avatar

Rory Franklin roryfranklin

View GitHub Profile
irb(main)> user = User.first
User Load (2.9ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ? [["LIMIT", 1]]
=> #<User id: 1, created_at: "2017-07-13 18:28:46", updated_at: "2017-07-13 18:28:46">
irb(main)> user.reviews.first.author.object_id == user.object_id
CarReview Load (1.4ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."author_id" = ? ORDER BY "reviews"."id" ASC LIMIT ? [["author_id", 1], ["LIMIT", 1]]
=> true
irb(main)> user.cars.first.user.object_id == user.object_id
Car Load (1.7ms) SELECT "cars".* FROM "cars" WHERE "cars"."user_id" = ? ORDER BY "cars"."id" ASC LIMIT ? [["user_id", 1], ["LIMIT", 1]]
=> true
class User < ApplicationRecord
has_many :reviews, inverse_of: 'author', foreign_key: 'author_id'
has_many :cars
end
irb(main)> user = User.first
User Load (1.6ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT ? [["LIMIT", 1]]
=> #<User id: 1, created_at: "2017-07-13 18:28:46", updated_at: "2017-07-13 18:28:46">
irb(main)> user.reviews.first.author.object_id == user.object_id
Review Load (2.7ms) SELECT "reviews".* FROM "reviews" WHERE "reviews"."author_id" = ? ORDER BY "reviews"."id" ASC LIMIT ? [["author_id", 1], ["LIMIT", 1]]
User Load (6.3ms) SELECT "users".* FROM "users" WHERE "users"."id" = ? LIMIT ? [["id", 1], ["LIMIT", 1]]
=> false
irb(main)> user.cars.first.user.object_id == user.object_id
Car Load (1.7ms) SELECT "cars".* FROM "cars" WHERE "cars"."user_id" = ? ORDER BY "cars"."id" ASC LIMIT ? [["user_id", 1], ["LIMIT", 1]]
=> true
user = User.create
5.times do |i|
Review.create(author: user)
Car.create(user: user)
end
@roryfranklin
roryfranklin / car.rb
Created July 14, 2017 09:55
Car Review and Ownership modelling
class Car < ApplicationRecord
belongs_to :user
end
@roryfranklin
roryfranklin / dump.node.js
Created June 12, 2012 11:06 — forked from natevw/dump.node.js
2couchbase2 - dump everything from CouchDB into Couchbase (via memcached interface)
var asy = require('async'),
mc = require('mc'),
f = require('fermata');
var DB = 'test';
var client = new mc.Client(),
source = f.json("http://localhost:5984")(DB);
var page = {limit:5};
class Event < CouchRest::Model::Base
use_database DB
property :event_type, :type => String
property :actioner_id, :type => String
property :event_date, :type => String
property :event_parent, :type => String
property :event_hierarchy, :type => [String], :default => []
property :old_state
property :new_state