Skip to content

Instantly share code, notes, and snippets.

View arrowcircle's full-sized avatar

Oleg Bovykin arrowcircle

  • Ivy Knob
  • Berlin, Germany
View GitHub Profile
@arrowcircle
arrowcircle / race.rb
Created October 8, 2012 18:25
racecondition
def update_audio_marker
transaction do
marker = available_markers.order(:date).lock(true).try(:first)
if marker
marker.complete_markers += 1
marker.calculate
marker.save!
end
end
end
@arrowcircle
arrowcircle / gist:3893778
Created October 15, 2012 17:22
whats better?
money_development=> explain analyse insert into audio_markers (user_id, total_markers, date) SELECT users.id, count(*) as count, '2012-10-15' FROM users INNER JOIN portfels ON portfels.user_id = users.id where portfels.status = 6 and portfels.valid_til is not null group by users.id;
QUERY PLAN
----------------------------------------------------------------------------------------------------------------------------------------------------------------------
Insert on audio_markers (cost=844.24..844.27 rows=1 width=12) (actual time=4.175..4.175 rows=0 loops=1)
-> Subquery Scan on "*SELECT*" (cost=844.24..844.27 rows=1 width=12) (actual time=4.107..4.111 rows=2 loops=1)
-> HashAggregate (cost=844.24..844.25 rows=1 width=4) (actual time=4.079..4.079 rows=2 loops=1)
-> Nested Loop (cost=227.27..844.24 rows=1 width=4) (actual
module.exports = function(ws) {
ws.onopen = function() {
ws.send('biff');
};
ws.onmessage = function(m) {
console.log(m);
};
};
gem 'piet'
User.find_by_sql("insert into usercalcs (username, amount, created_at, updated_at, flag) select u.username::integer, sum(t.amount) as tr_sum, '#{t}', '#{t}', 0 from users u join archive_transfers t on u.username = t.receiver_id where t.status = 2 and t.currency = 1 and t.created_at between '#{s}' and '#{e}' group by u.username")
= cache todo do
= todo_li_tag todo do
.nubbin{"data-behavior" => "nubbin hover_content"}
.spacer
= link_to_trash [ @project, todo ], confirm: "Are you sure?"
= link_to_remote_edit "Edit", [ :edit, @project, todo ] unless todo.completed?
%div{class: ["#{'complete' if todo.completed}"]}
%span.wrapper
= todo_check_box_tag todo
def subtree_ids_sql(depth)
tree_sql = <<-SQL
WITH RECURSIVE search_tree(id, path, depth) AS (
SELECT id, ARRAY[id], 1
FROM users
WHERE id = #{id}
UNION ALL
SELECT users.id, path || users.id, search_tree.depth + 1
FROM search_tree
JOIN users ON users.parent_id = search_tree.id
# coding: utf-8
# app/models/api/user/registration.rb
module Api::User::Registration
def self.included(base)
base.extend(ClassMethods)
end
module ClassMethods
#coding: utf-8
module WalletOperations
def add(target, amount)
res = false
if (eval("#{self.class.name.constantize}::TARGETS").include? target) && (amount.to_i > 0)
sql = <<-SQL
UPDATE #{self.class.table_name} SET "#{target.to_s}" = "#{target.to_s}" + #{amount}, updated_at = now() WHERE id = #{self.id}
SQL
ActiveRecord::Base.connection.execute(sql)
res = true
@arrowcircle
arrowcircle / cml2.rb
Created April 23, 2013 14:47 — forked from danil/cml2.rb
module CML2
# Includes to ProductCategory model
module ProductCategory
def import node_set
::ProductCategory.delete_all
::ProductCategory.import_categories \
node_set.xpath('/КоммерческаяИнформация/Классификатор/Группы')
end