Sometimes I like to pop open the console script that comes with Rails to test small excerpts of code. That code normally involves some more involved ActiveRecord queries. Although not an expert in ActiveRecord, I'm proficient with SQL and want to see what it's translating underneath the hood for efficiency purposes. This will help me refactor or rethink how I'm writing my app if it looks inefficient. Now when the query is in the actual application itself, it all shows up in logs. Ad-hoc ActiveRecord queries in the console do not though. Anyway to change that behavior?
views:
89answers:
2
+8
A:
Enter this line in the console:
ActiveRecord::Base.logger = Logger.new(STDOUT)
John Topley
2010-05-29 17:44:29
Perfect, just what I needed. Have any recommendation where to go find small tricks such as these are documented?
randombits
2010-05-29 18:18:59
Sure. http://slash7.com/2006/12/21/secrets-of-the-rails-console-ninjas/ and http://stackoverflow.com/questions/123494/whats-your-favourite-irb-trick Plus http://railscasts.com/ is always good.
John Topley
2010-05-29 18:35:49
A:
Ad-hoc ActiveRecord queries in the console do not though. Anyway to change that behavior?
Are you sure about that? I use the console all the time for things like this. For the development environment, the default behavior is to log queries to development.log. Are you sure you didn't change your log level in your environment?
Do you see the following in development.rb?
config.log_level = :debug
jdl
2010-05-29 18:15:52