Fixing Rails nested attributes on collections with sti

user.persons.build(:type=>’Manager’).class != Manager, which makes a lot of problems with single table inheritance, especially when dealing with accepts_nested_attributes_for / fields_for that rely on correct class of the associated.

# Make build associations have their given type
# Makes that: user.persons.build(:type=>Manager).class == Manger
class ActiveRecord::Reflection::AssociationReflection
  def build_association(*options)
    if options.first.is_a?(Hash) and options.first[:type].presence
      options.first[:type].to_s.constantize.new(*options)
    else
      klass.new(*options)
    end
  end
end

rails issue

one-liner to install nginx with passenger and ssl

Just build this little guy for our puppet script, maybe someone needs it too 🙂

export V=0.7.67 && wget -O /tmp/nginx-$V.tar.gz http://sysoev.ru/nginx/nginx-$V.tar.gz && cd /tmp && tar xzvf nginx-$V.tar.gz && sudo passenger-install-nginx-module --nginx-source-dir /tmp/nginx-$V --extra-configure-flags="--with-http_ssl_module" --auto --prefix=/opt/nginx-$V && rm /opt/nginx && sudo ln -s  /opt/nginx-$V /opt/nginx

(If you like the stock nginx without ssl, youll need the –auto-download option)

Maximum and minimum for MongoMapper

A small hack to get minimum/maximum functionality on MongoMapper, its not 100% secure(can be wrong when inserting twice at the same time etc), but good enought for our usecase 😉

Usage

  User.maximum(:friend_count)
  User.minimum(:friend_count)

Code

module MongoMapper::Document::ClassMethods
  def maximum(column)
    first(:order => "#{column} desc").try(column)
  end

  def minimum(column)
    first(:order => "#{column} asc").try(column)
  end
end