How to see MySQL queries in rails console
Yes, this can be achieved through redirecting rails log to standard output.
Write these in your console prompt:
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.connection_pool.clear_reloadable_connections!
Furthermore, you can put these lines in ~/.irbrc file, so that each time you don't need to manually write these 2 lines:
require 'rubygems'
if ENV.include?('RAILS_ENV') && ENV["RAILS_ENV"] == 'development'
ActiveRecord::Base.logger = Logger.new(STDOUT)
ActiveRecord::Base.connection_pool.clear_reloadable_connections!
end
Hope this helps...
In Rails 3+ you can use ActiveRecord::Relation’s to_sql
method:
User.where(:id => 3).to_sql
#=> "SELECT \"users\".* FROM \"users\" WHERE \"users\".\"id\" = 3"