Various bug fixes for activerecord