SQL calls and associations

[...]

http://railscasts.com/episodes/22 is what you're looking for.