ActiveRecord without default scope

This is a pretty hacky implementation of the feature we needed:
remove the default scope for some queries (e.g. update deleted records)

Code:

class ActiveRecord::Base
  def self.without_default_scope(&block)
    scope = scoped({})
    defaults = scope.current_scoped_methods_when_defined

    old = defaults[:find][:conditions]
    defaults[:find][:conditions] = {}

    begin
      yield(scope)
    ensure
      defaults[:find][:conditions] = old
    end
  end
end

Usage

MYModel.without_default_scope{|s| s.update_all(:deleted_at => nil)}

There is an less hacky alternative, but it did not work for us (AR 2.3.2)

Joined environment file for production and staging

Often production and staging share much, so why not use a common file for that…

#config/environments/production.rb and config/environments/staging.rb
eval(File.read("#{File.dirname(__FILE__)}/production_like.rb"))
...environment specific code ...

#config/environments/production_like.rb
config.cache_classes = true
...

the File.read/eval is rather hacky, but works nevertheless ๐Ÿ˜‰

Hash values through query like nested keys

How to get “x[y][z]” out of a hash ?

This made life with our form helpers a lot easier, since they take normal or nested keys <-> name of the input and prefilled value through params.

Any alternative is appreciated, since its pretty complex/hacky, the name is weird too …
(a simple to_query.split(‘=’)).inject… can work too, but would stringify all values and keys)

Usage

{'x'=>{'y'=>{'z'=>1}}}.value_from_nested_key('x[y][z]') == 1

Code

class Hash
  # {'x'=>{'y'=>{'z'=>1}}.value_from_nested_key('x[y][z]') => 1
  def value_from_nested_key(key)
    if key.to_s.include?('[')
      match, first, nesting = key.to_s.match(/(.+?)\[(.*)\]/).to_a
      value = self[first]
      nesting.split('][').each do |part|
        return nil unless value.is_a?(Hash)
        value = value[part]
      end
      value
    else
      self[key]
    end
  end
end