Wednesday, May 4, 2011

Speed up your counts in Rails 3

Just bumped into this and thought I would share. If you are trying to find the number of records that match two columns in your schema you might do this:

find_all_by_some_column_and_some_other_column('val', 'val2').count

What this does is select all of the data that matches and then does a count in Ruby. This is slower because it retrieves all of the data for every matching record. Whereas if you count like this:

where(:some_column => 'val', :some_other_column => 'val2').count

It will do the count in the database (which databases are really good at) and only return the total number of records that match. If you have 12 records it won't matter much which of the above you use. If you have 12,000 records the second will be incredibly faster.

Monday, May 2, 2011

5 Alternate Uses For GMail

GMail is the ultimate email client. Humorously, the capabilities that are so great for email actually enable several other "features".

1) Note taking - Any time I think of something interesting that I may want to remember, I write myself an email with the subject starting with "howto:". A couple that I visit often are "howto: make SEO work", "howto: deal with meetings" and "howto: amuse users while they are waiting"

2) Bookmarks - I never could get into sites like del.icio.us. Something about going to another site to create a bookmark just didn't make sense. I tried to use the bookmarklets and the browser plugins, but nothing made sense. Then one day I searched for a note that I knew had a link in it because I wanted to find the link. That was when I started sending emails to myself with keywords in the email to help find the link.

3) To do's - To do's are just short lived notes. I almost always delete them after they have been completed. For some reason actual to do lists or to do list software (web based or fat client) just doesn't work for me. I have started to believe that I have to-do-list-ophobia. But if I send myself an email then I am impacting my zero inbox beliefs so getting that to-do email out of my inbox is a priority. Also, I am always checking my email so the to-do is always available, whereas if the to-do list is in software, I have to actively go looking for it. To-do's even have their own tag with the most annoying color combination.

4) File transfer - I can't count the number of times I have used GMail to transfer files from one computer to another. I have emailed songs, books and of course word documents. This "feature" may seem weak, but I would argue it is so strong that you don't even know it is is a feature. You just expect email to send files. Heck, when Gmail came out one of the first things people tried to do with it was put a file system on top of it. Of course everyone is using dropbox for this now, but I still use GMail from time to time.

5) Tell me about your alternate uses for GMail!