0

I don't know whether this is a reasonable request. I hope to see the result in the log file such as log/development.log, Now the ActiveRecord::Base.logger.level = 0 and there is only the SQL statements:

User Load (0.6ms)  SELECT "users".* FROM "users" WHERE "users"."id" = 3 LIMIT 1
Excursion Load (0.3ms)  SELECT "excursions".* FROM "excursions" WHERE  "excursions"."id" = $1 LIMIT 1  [["id", "5"]]
Actor Load (0.5ms)  SELECT "actors".* FROM "actors" WHERE "actors"."id" = 4 LIMIT 1

There is no results. I know in the 'rails console', when using ActiveRecord::Base.logger = Logger.new(STDOUT), I see the statements and corresponding results:

1.9.3-p551 :001 > ActiveRecord::Base.logger = Logger.new(STDOUT)
=> #<Logger:0x007f80441364d0 @progname=nil, @level=0,  @default_formatter=#<Logger::Formatter:0x007f8044135c60 @datetime_format=nil>, @formatter=#  <Logger::SimpleFormatter:0x007f8044134bd0 @datetime_format=nil>, @logdev=#<Logger::LogDevice:0x007f8044135440 @shift_size=nil, @shift_age=nil, @filename=nil, @dev=#<IO:<STDOUT>>, @mutex=# <Logger::LogDevice::LogDeviceMutex:0x007f8044135378 @mon_owner=nil, @mon_count=0, @mon_mutex=#<Mutex:0x007f8044134c98>>>> 
1.9.3-p551 :002 > User.first
User Load (1.0ms)  SELECT "users".* FROM "users" LIMIT 1
Actor Load (0.8ms)  SELECT "actors".* FROM "actors" WHERE   "actors"."id" = 2 LIMIT 1
ActivityObject Load (0.9ms)  SELECT "activity_objects".* FROM "activity_objects" WHERE "activity_objects"."id" = 2 LIMIT 1
=> #<User id: 1, encrypted_password:  "$2a$10$wFj.Q9XX0ua16BF0.SIps.VjnfVi8/6egicirc/SFxw3...", password_salt: nil, reset_password_token: nil, reset_password_sent_at: nil, remember_created_at: nil, sign_in_count: 0, current_sign_in_at: nil, last_sign_in_at: nil, current_sign_in_ip: nil, last_sign_in_ip: nil, authentication_token: nil, created_at: "2015-03-25 22:14:32", updated_at: "2015-03-25 22:14:32", actor_id: 2, language: nil, connected: false, status: "chat", chat_enabled: true, occupation: nil, invitation_token: nil, invitation_created_at: nil, invitation_sent_at: nil, invitation_accepted_at: nil, invitation_limit: nil, invited_by_id: nil, invited_by_type: nil> 

What options I can set for this? Thanks in advance!

abelard2008
  • 1,984
  • 1
  • 20
  • 35
  • Does this help you? http://stackoverflow.com/questions/8096393/how-to-show-sql-result-on-server-console – fatfrog Mar 27 '15 at 03:02

1 Answers1

0

I researched some ActiveRecord (v3.2.21) code, It seems that there is no option about the SQL result. The above log User Load (1.0ms) SELECT "users".* FROM "users" LIMIT 1 is finished through callingActiveSupport::Notifications.instrument():

        # query_cache.rb
        def cache_sql(sql, binds)
      result =
        if @query_cache[sql].key?(binds)
          ActiveSupport::Notifications.instrument("sql.active_record",
            :sql => sql, :binds => binds, :name => "CACHE", :connection_id => object_id)
          @query_cache[sql][binds]
        else
          @query_cache[sql][binds] = yield
        end
      result.collect { |row| row.dup }
    end

So if I want to see the result in log I can add a log statement:

logger.info "#{result}"
result.collect { |row| row.dup }
abelard2008
  • 1,984
  • 1
  • 20
  • 35