Content

the single best active record learning tool that exists. period. the end.

27 October 2009 // Filed under active record

Every once in a while I see this little code snippet show up somewhere:

script_console_running = ENV.include?('RAILS_ENV') &&
                         IRB.conf[:LOAD_MODULES] &&
                         IRB.conf[:LOAD_MODULES].include?('console_with_helpers')
rails_running = ENV.include?('RAILS_ENV') &&
                !(IRB.conf[:LOAD_MODULES] &&
                IRB.conf[:LOAD_MODULES].include?('console_with_helpers'))
irb_standalone_running = !script_console_running && !rails_running
if script_console_running
  require 'logger'
  Object.const_set(:RAILS_DEFAULT_LOGGER, Logger.new(STDOUT))
end

You see, if you drop this little gem in ~/.irbrc you’ll start getting the SQL output from your various ruby commands. This is an excellent debugging tool, but it’s an even better learning tool.

To the novice the difference between the following two statements may not be readily apparent…

User.find(:first).posts.each{|p| p.comments.do_something}
User.find(:first, :include => {:posts => :comments}).posts.each{|p| p.comments.do_something}

…but as soon as they sit and watch what scrolls by in the terminal they will *very* quickly realize that one of them is *very* wrong.

I submit that the next time you are teaching anyone the basics of AR that this be the very first thing that you introduce them to.

2009-10-27  ::  admin

Share your thoughts







Tags you can use (optional):
<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>