Thursday, April 3, 2008

Eager Loading + Order By

So I was trying to cut down on the number of queries that I was making on a particular page and ran into a slight problem.

Basically, I had a relationship where a User has many Notifications:

class User
has_many :notifications, :order => 'created_at DESC'
end

In my controller, I wanted to cut down on the queries, so I did some eager loading:

@user = User.find(user_id, :include => :notifications)

However, when I went to go view my page, the notifications were not in the order as I specified. I went to go take a look at the query, and lo-and-behold, there was no ORDER BY stated in the query.

After some thought, it became apparent why no ORDER BY was included. This makes sense because if you eager load many tables, each with its own ORDER BY, it would be difficult, if not impossible, to have everything returned properly.

I would assume, without much thought, that one would assume that this would work. With further thought though, this becomes the correct thing to do.

Just a heads up ;)


W

Tuesday, April 1, 2008

BackgrounDRb

There are often times when you want to perform scheduled tasks, such as email reminders. These tasks are usually triggered at a specific time, and not necessarily by a user action.

On a unix machine, cron jobs do exactly this. They are triggered at a particular time, do their work, then go away. I looked around for a bit to see if there was a rails plugin to handle jobs like these.

Behold: backgrounDRb. This is a rails plugin that runs its own server to do a multitude of jobs. It supports cron jobs, but at the same time, it handles jobs in the background. For instance, if you are generating a large document, you don't want the user to wait for a long time. Instead, it will spawn a job in the background, allow the user to continue doing his/her stuff, and periodically check when the job is done.

For my purposes, I only needed the simple ability to create cron jobs, so I have yet to experiment with the background tasks. However, I found it to be super easy to use and everything is very straightforward.

If you're looking for a scheduler type plugin, look no further. This baby is amazing!


W

Monday, March 31, 2008

Textmate: Ubuntu style!

If you've ever watched any railscasts, most of them use something called Textmate to edit their files on a Mac. Apparently, it's suppose to be really good.

Well, recently, I've been getting really fed up with Aptana and RadRails. There are lots of things that bug me, like the wait time to save a file and especially the wait time to copy/paste things. It's just horrible. Furthermore, RadRails decides to crap out on me every so often. There are so many errors in the background that I run out of memory. So far, there doesn't seem to be a fix.

Enough of that... I'm moving on to better things, like a Textmate clone: a revved up version of Gedit! Apparently, you can easily make Gedit behave very similarly to Textmate! I followed this guide and so far, everything is smooth.

I haven't tried developing yet. I just installed everything. I'll let you guys know how it goes... but I'm anticipating I'll like this more than RadRails. Only downside is the loss of the SVN plugin and a debugger... I wonder...


W

Ruby Exception Hiearchy

For those who wants a better understand of the exception hiearchy, here it is!

Exception
Errno
NoMemoryError
ScriptError
LoadError
NotImplementedError
SyntaxError
SignalException
Interrupt
StandardError
ArgumentError
IOError
EOFError
IndexError
LocalJumpError
NameError
NoMethodError
RangeError
FloatDomainError
RegexpError
RuntimeError
SecurityError
SystemCallError
SystemStackError
ThreadError
TypeError
ZeroDivisionError
SystemExit
SystemStackError

Cheers,

Joseph

Sunday, March 30, 2008

Ubuntu beats out Mac and Vista

The results are final from the 3-day-long hack-fest and guess who came out on top? Yup, that would be Ubuntu!

A hacking contest was held in Vancouver recently which featured 3 laptops, 1 was a MacBook Air, the other a Vista, and the 3rd was an Ubuntu.

The first to be compromised was the MacBook Air. Amazingly, Vista held its ground until the 3rd day, where it was finally pwned.

More details can be found here:

http://dvlabs.tippingpoint.com/blog/2008/03/28/pwn-to-own-final-day-and-wrap-up


W

Friday, March 28, 2008

Named Routes: url vs path

So I spent a good 30 minutes trying to figure out why my links were missing the host part of the URL.

What I was trying to do was send emails using ActionMailer and generating urls inside the ActionMailer instead of the ActionController. The documentation says that you can use url_for and named routes if you do what it says.

Well, for a while there, I was completely stumped as to why my named routes were missing the host part, until I realized I was using the path method for named routes instead of the url method:

users_path #/users
users_url #http://www.mysite.com/users

The difference is obvious. However, if you're used to using path instead of url, you may be stumped as well. Hopefully, you guys won't have to go through the trouble I did ;)


W

Friday, March 21, 2008

Ubuntu + Postfix

So I researched a little bit about setting up an email server. I at least got something going, as I could send email out. However, I am still using my ISP's server as a relay host, as they block port 25.

On Ubuntu, I set up Postfix by following the documentation.

Then, to use a relay host, I simply went into /etc/postfix/main.cf and entered the server's address into the relayhost variable.

A quick reload of the Postfix server:

>sudo /etc/init.d/postfix reload

and all was right with the world.

To set up POP3 and IMAP, take a look at the following documentation.

Enjoy!


W