Posts about Tips & Tricks

Using Postgres Enum in Rails ActiveRecord

Oct 16, 2020

In this post, I will provide some code to make working with an Enum data type in Postgres easier within your ActiveRecord models. Skip to the end for the code, or stick around for some verbose pontificating.

Read more

HOWTO - Get a list of a class's subclasses

Feb 5, 2009

I recently came across a situation where I had an AbstractClass, an I wanted to know all of the classes that had inherited from it. There were lots of implementations on the web, but that weren't exactly what I wanted, or they used ObjectSpace to get ALL the classes, and see if the interesting one was in its ancestors.

Read more

Rails Logging to Syslog using Logging gem

Mar 6, 2008

When using a mongrel cluster, you can either log to a separate file for each mongrel instance, or you can log them all to the same file, but on a loaded cluster, there's a good chance your logged lines will get interleaved and be unreadable. Luckily, there's another way. The new replacement for log4r Logging can take care of this. It has a built-in way of not interleaving the lines, but (I think) its using lockfiles to do so, and if so, that's going to be detrimental to performance. The best solution has been around for 25 years, syslog. And with one of the more recent syslog daemons (syslog-ng, or rsyslog), you can set it up to log your mongrel log wherever you like.

Read more

Ruby rescues are not slow

Nov 27, 2007

I've heard several times that you should avoid exceptions because they are slow. They are in Java, so I think that has given them a bad name everywhere. The only real numbers I could find are from . His 'plain' test is also missing the conditional that would also have to be executed (In this case, to make sure 5 is not 0). His post is also 18 months old, so I updated the 'plain' test and re-ran it (Upping the runs to 5,000,000). My plain test now looks like:

Read more

Comcast & Bittorrent

Oct 27, 2007

Comcast user? Bummed that they're ruining your bittorrent? No problem. They way the kill your connection is by sending unsolicited TCP reset packets to your bittorrent client, making it think that the person you're downloading from has closed the connection. If you're using a good firewall/router, you can write a rule that will block it. I'm using Tomato firmware for my Linksys WRT54G, and added the following line to my firewall scripts: iptables -A {wan interface} -p tcp --dport {bittorrent port} --tcp-flags RST RST -j DROP This drops all incoming RST packets to your bittorrent client. Now, this even removes the legitimate ones, if your source really does disconnect, you won't know about it. Luckily, the connection will timeout after about 10 minutes anyways, so its not that bad. I've been using this for a couple weeks now, and my bittorrent transfer speeds are back to what they were before Comcast started doing all this.

Read more