Skip to content

Instantly share code, notes, and snippets.

Keybase proof

I hereby claim:

  • I am tarmo on github.
  • I am tarmo_t (https://keybase.io/tarmo_t) on keybase.
  • I have a public key ASChcjqrX0DxB87JEGq1PFdQQRl3OZOjsbZJxdLnG6uubAo

To claim this, I am signing this object:

#!/usr/bin/env ruby
require 'yaml'
class Hash
def sort_by_key(recursive = false, &block)
self.keys.sort(&block).reduce({}) do |seed, key|
seed[key] = self[key]
if recursive && seed[key].is_a?(Hash)
seed[key] = seed[key].sort_by_key(true, &block)
#!/usr/bin/env ruby
require 'yaml'
def flatten_hash(hash, result={}, root=nil)
hash.each do |k,v|
key = "#{root}#{root ? "." : ""}#{k}"
if v.kind_of?(Hash)
flatten_hash(v, result, key)
From 7f49582ac214d20258dccd222e360350591e1cce Mon Sep 17 00:00:00 2001
From: =?utf-8?q?Tarmo=20T=C3=A4nav?= <tarmo@itech.ee>
Date: Wed, 23 Jul 2008 14:57:42 +0300
Subject: [PATCH] has_many association and named_scope empty?() uses exists?({}) instead of counting.
This way the database only has to find one match using an index
instead of counting all the matches. If the association or scope
is already loaded or the association has a custom counter_sql
size check is used instead.
---
diff --git a/activerecord/lib/active_record/associations/association_collection.rb b/activerecord/lib/active_record/associations/association_collection.rb
index a28be9e..9c44eb9 100644
--- a/activerecord/lib/active_record/associations/association_collection.rb
+++ b/activerecord/lib/active_record/associations/association_collection.rb
@@ -206,7 +206,11 @@ module ActiveRecord
end
def empty?
- size.zero?
+ if loaded? || @reflection.options[:counter_sql]
2) Error:
test_counting_using_sql(HasManyAssociationsTest):
ActiveRecord::StatementInvalid: PGError: ERROR: subquery must return only one column
: SELECT "companies".id FROM "companies" WHERE (SELECT * FROM companies WHERE client_of = 1) AND ( ("companies"."type" = 'Client' OR "companies"."type" = 'SpecialClient' OR "companies"."type" = 'VerySpecialClient' ) ) LIMIT 1
1) Error:
test_joins_with_namespaced_model_should_use_correct_type(HasManyAssociationsTest):
ActiveRecord::StatementInvalid: PGError: ERROR: column "companies.type" must appear in the GROUP BY clause or be used in an aggregate function
: SELECT companies.*, COUNT(companies.id) AS num_clients FROM "companies" INNER JOIN "companies" clients_companies ON clients_companies.firm_id = companies.id AND "clients_companies"."type" = E'Namespaced::Client' WHERE ("companies"."id" = 16) AND ( ("companies"."type" = 'Namespaced::Firm' ) ) GROUP BY companies.id
./test/cases/../../lib/active_record/connection_adapters/abstract_adapter.rb:160:in `log'
./test/cases/../../lib/active_record/connection_adapters/postgresql_adapter.rb:488:in `execute_without_query_record'
./test/cases/helper.rb:38:in `execute'
./test/cases/../../lib/active_record/connection_adapters/postgresql_adapter.rb:945:in `select_raw'
./test/cases/../../lib/active_record/connection_adapters/postgresql_adapter.rb:932:in `se