Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

First thing, when you use activerecord, do you know and use the include option when doing find?

An example, suppose you have two models Professor, and Lessons A lesson belongs to a professor and a professor has many lessons.

Now, a common mistake is to do things like @professors = Professor.find(:all)

and later to have some code that does something like

@professors.each do |professor| ... professor.lessons ... end

What happens? well you have one request to the database and then one request for each professor. (so the number of request is equal to the number of professors + 1).

If however you do Professor.find(:all, :include = "lessons") it will fetch the lessons right from the beginning and your code will only do one request....

There are a lot of small mistakes like that that beginners do.... And while rails isolate you from the database stuff, you still need to understand what it does if you want things to work well

For more information on all this: http://i.nfectio.us/articles/2006/09/20/mysql-query-analyzer-rails-plugin it's a plugin that you can use to analyze what mysql did (it uses the explain statement for this

http://railsexpress.de/blog/ This blog is a good read.

Cheers....



Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: