Tuesday, March 31, 2009

Ruby on Rails Order By Using Associated Model

It is very easy to use an 'order by' clause in a model in Ruby on Rails. Supposing you have a Category model it's as simple as doing this:

@categories = Category.find(:all, :order => 'title')

But what if I need to order by the title of the subcategories after ordering by the categories title?

Using the category_id does not work, because it is almost sure that the categories will not be added in alphabetical order, so the command below does not work for me:

@subcategories = Subcategory.find(:all, :order => 'category_id,title')

And neither would work the sentence below, because the resulting SQL query will fail:

@subcategories = Subcategory.find(:all, :order => 'category.title, title')

This last command will fail because rails does not recognize the Category model inside the subcategories model. So all you have to do is tell Rails that you want to include another model in the query, and this is done using the :include option from the find method:

@categories = Category.find(:all,:include => :subcategories, :order => 'categories.title, subcategories .title')

It is easy after you find out how! ;)

This article can also be found on definenull.com.

Friday, March 13, 2009

Sun Java One 2009 - topics released

Sun has released the topics for the Java One 2009 Conference, with four high-level areas: Mobility, Rich Media Applications and Interactive Content, Services and Core Content.

It will happen in San Francisco in June, 2nd-5th.

Saturday, March 7, 2009

Cygwin personal setup

Some time ago I wrote a post on how to setup the looks of your Cygwin bash shell using rxvt. Well, I normally add a few more settings to my environment just to make it more useful.

I add the following lines to my ~/.bash_profile with some aliases to make it easier with directory surfing.

alias ls='ls --color=auto'
alias ll='ls -lsa'
alias l='ls -la'

It is also useful to set some preferences for vi, and this is done in the ~/.vimrc file:

set textwidth=70 background=dark
set is hls ic scs aw ruler ls=2
syntax on

I also like to create symbolic links at my home dir to allow me to have quick access to some of the original and most used folders in Windows, like:

ln -s /cygdrive/c/Documents\ and\ Settings/dambrosio/Desktop/ windesk
ln -s /cygdrive/c/Documents\ and\ Settings/dambrosio/Meus\ documentos/ mydocs

Some other useful symbolic links might be the ones pointing to your drives, because it is faster to type cd ~/c than cd /cygdrive/c

Quick and clean! ;)

Find this article also on definenull.com.

Tuesday, March 3, 2009

Change Default Date Format in Ruby on Rails

Just a quick and simple hint!

If you need to set the default formatting of dates throughout your whole application in Ruby on Rails, just add the following line to your "<your application>/config/environment.rb"

my_date_formats = { :default => '%d/%m/%Y' }



Note 1: Rails uses this :default format when attempting to write dates do the database, so be sure that your DBMS is able to use this new format

Note 2: you will need to restart your server so the changes will be effective.

Find this article also on Define Null.