Skip to content

Instantly share code, notes, and snippets.

class Carrot < ActiveRecord::Base
belongs_to :bunny
end
@Chryus
Chryus / gist:8899641
Last active August 29, 2015 13:56
Data modeling in rails: example of has_many :through migrations
class CreateCarrots < ActiveRecord::Migration
def change
create_table :carrots do |t|
t.string :color
t.timestamps
end
end
end
class CreateBunnies < ActiveRecord::Migration
@Chryus
Chryus / gist:8899960
Last active August 29, 2015 13:56
Data modeling in rails: example of has_many :through model associations
class Carrot < ActiveRecord::Base
has_many :communities
has_many :rabbits, :through => :communities
end
class Bunny < ActiveRecord::Base
has_many :communities
has_many :carrots, :through => :communities
end
@Chryus
Chryus / gist:8900461
Last active August 29, 2015 13:56
Exploring has_many :through relationship in the console
2.0.0-p247 :042 > bunny = Bunny.first
Bunny Load (0.2ms) SELECT "bunnies".* FROM "bunnies" ORDER BY "bunnies"."id" ASC LIMIT 1
=> #<Bunny id: 9, name: "Jasper", created_at: "2014-02-09 15:01:25", updated_at: "2014-02-09 15:01:25">
2.0.0-p247 :043 > bunny.carrot_ids
(0.1ms) SELECT "carrots".id FROM "carrots" INNER JOIN "communities" ON "carrots"."id" = "communities"."carrot_id" WHERE "communities"."bunny_id" = ? [["bunny_id", 9]]
=> []
2.0.0-p247 :044 > bunny.carrot_ids = [9, 10]
Carrot Load (0.3ms) SELECT "carrots".* FROM "carrots" WHERE "carrots"."id" IN (9, 10)
@Chryus
Chryus / gist:8900472
Last active August 29, 2015 13:56
Exploring has_many :through relationship in rails console
2.0.0-p247 :053 > carrot = Carrot.first
Carrot Load (0.1ms) SELECT "carrots".* FROM "carrots" ORDER BY "carrots"."id" ASC LIMIT 1
=> #<Carrot id: 9, color: "green", created_at: "2014-02-09 15:02:04", updated_at: "2014-02-09 15:02:04">
2.0.0-p247 :054 > carrot.bunny_ids
(0.1ms) SELECT "bunnies".id FROM "bunnies" INNER JOIN "communities" ON "bunnies"."id" = "communities"."bunny_id" WHERE "communities"."carrot_id" = ? [["carrot_id", 9]]
=> [9]
2.0.0-p247 :058 > carrot.bunnies.collect{|bunny| bunny.name}
=> ["Jasper"]
@Chryus
Chryus / gist:8900766
Created February 9, 2014 15:35
Access resources via has_many :through in rails console
bunny2 = Bunny.find_by name: "Hakeem"
Bunny Load (0.2ms) SELECT "bunnies".* FROM "bunnies" WHERE "bunnies"."name" = 'Hakeem' LIMIT 1
=> #<Bunny id: 13, name: "Hakeem", created_at: "2014-02-09 15:01:53", updated_at: "2014-02-09 15:01:53">
2.0.0-p247 :091 > bunny2.carrot_ids
(0.1ms) SELECT "carrots".id FROM "carrots" INNER JOIN "communities" ON "carrots"."id" = "communities"."carrot_id" WHERE "communities"."bunny_id" = ? [["bunny_id", 13]]
=> []
2.0.0-p247 :092 > bunny2.carrot_ids = [9,10]
Carrot Load (0.3ms) SELECT "carrots".* FROM "carrots" WHERE "carrots"."id" IN (9, 10)
@Chryus
Chryus / gist:9547363
Last active August 29, 2015 13:57
Test: I have an array of stock prices, where the index value corresponds to the time in minutes after midnight, and the value corresponds to the stock prices. Calculate the maximum profit i could make from the buy and sell of the stock.
stock_prices = [1, 5, 8, 3, 13, 18, 11, 44]
def max_profit stock_prices
profit_so_far = 0
stock_prices.each_with_index do |buy_price, buy_time|
stock_prices.each_with_index do |sell_price, sell_time|
current_profit = buy_price - sell_price
if current_profit > profit_so_far && buy_time < sell_time
profit_so_far = current_profit
end
class Node
attr_reader :content
attr_accessor :neighbor
def initialize(x)
@content = x
@neighbor = nil
end
class LinkedList
attr_reader :root
def initialize(node)
@root = node
end
end
class LinkedList
attr_reader :root
def initialize node
@root = node
end
def add x
current = root