Rails Tip: See ActiveRecord’s SQL Statements log output under Rails Console (rails console)

A good trick for prototyping/testing ActiveRecord is to have the SQL statement output to your console as you work. It could be achieved so with:

$ rails c
Loading development environment (Rails 3.0.10)
ruby-1.8.7-p352 :001 > ActiveRecord::Base.logger = Logger.new(STDOUT)

Whenever your code relates to AR, the SQL statement will be piped directly to STDOUT:


ruby-1.8.7-p352 :002 > Person.find 1
  Person Load (0.1ms)  SELECT "people".* FROM "people" WHERE "people"."id" = 1 LIMIT 1
 => #<Person id: 1, name: "one", age: 23, created_at...
Advertisements

About Jones Lee

Nothing much about me..

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: