Display SQL in Rails console
Type the following in your rails console
ActiveRecord::Base.logger = Logger.new STDOUT
and after runnining a query you’ll see SQL queries with the time it took to execute it in your irb session.
irb(main):001:0> User.last
User Load (0.6ms) SELECT "users".* FROM "users" ORDER BY "users"."id" DESC LIMIT 1
=> #<User id: 1, created_at: "2015-01-26 01:05:16", updated_at: "2015-02-22 22:19:53" ...>
You don’t have to type it everytime as you can put it into your ~/.irbrc
file. And it will load with every new irb session.
if defined?(Rails::Console)
ActiveRecord::Base.logger = Logger.new(STDOUT)
end
From now on you’ll have a full control over your active record queries and you’ll see all the SQL comming from them. No more n + 1 queries and no more mistakes. Pure power of raw SQL.