How to name RSpec describe blocks for methods

So far our convention has been

  describe :method_name do 
    ...
  end

But this has some obvious drawback like separation of instance/class methods.
I hope we can now all agree on the unambiguous convention Mr Heinrich found in a great rspec presentation:

  • use “description” for non-methods
  • use pound “#method” for instance methods
  • use dot “.method” for class methods

This also makes prettier specdoc output 🙂

MySql: 4 ways to lower the impact of TEXT columns

Starting point

Normally there is a table with some small stuff like id/type/xxx_id(~50B) and a text column(default 768B + x) that is not needed all the time, because most users enter less than 255 chars or most queries only need the smaller columns.

  • text bloats table size
  • text makes queries slow
  • text bloats mysql query cache
  • text is always loaded into buffer pool (consumes ram)

Alternative Row format

With row ROW_FORMAT=​DYNAMIC those default 768B+x can be reduced to a mere default 20B+x, but requires innodb_file​_format=Barracuda.

1 to 1 Relationship

Keep all small columns on the first model and pull in the text from another model e.g. using translated_attributes.
Generates some overhead in the model but is reasonably fast.

Varchar column as cache + 1 to 1 Relationship

Only fetches the text/adds the relationship when needed. Useful when only a few texts are larger then 255 chars and the content is often needed. e.g. on original model:

def text_truncated(to=255)
  text = if to > 255 and text_cache.size == 255
    1_to_1_relationship.text
  else
    text_cache
  end
  text.truncate(to)
end

(this needs truncate on string)
And e.g. in the UI the text could always show truncated to 255 chars and only on click it is really loaded from the db.

Using covering index

Make an index that covers all the normal columns except the text, and FORCE INDEX on the queries.

Performance

Source:
A lot more details on the 1-to-1-relation ship / covering index can be found here

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

Installing MongoDb on Ubuntu Hardy from source with init.d and data dir

After setting up mongo by hand for 2 servers that are not deployed via puppet, here is a bash script to do it…


sudo su root

cd /opt/ && /usr/bin/curl -OL http://downloads.mongodb.org/linux/mongodb-linux-x86_64-1.4.1.tgz && tar -zxvf mongodb-linux-x86_64-1.4.1.tgz && ln -s /opt/mongodb-linux-x86_64-1.4.1 /opt/mongodb

useradd mongod -u 5005

curl http://gist.github.com/raw/413433/834a3d0ecc96cb36f8918f6d01b65fc082025cc0/gistfile1.sh > /etc/init.d/mongodb

chown mongod:mongod /etc/init.d/mongodb
chmod 750 /etc/init.d/mongodb

mkdir /etc/mongodb
chown mongod:mongod /etc/mongodb
chmod 750 /etc/mongodb

echo "dbpath=/opt/mongodb-data/" > /etc/mongodb/mongodb.conf
chown mongod:mongod /etc/mongodb/mongodb.conf
chmod 750 /etc/mongodb/mongodb.conf

mkdir /opt/mongodb-data
chown mongod:mongod /opt/mongodb-data
chmod 750 /opt/mongodb-data