新手问题 怎么在 rails console 里显示 SQL 语句?

railsboy · September 15, 2017 · Last by zhihui replied at April 17, 2019 · 2701 hits

接触 ruby 时间不长,不忙的时候就看看项目中的代码,也在学习 ruby 基本知识。今天想着想在 irb 中打印下 sql 语句,比如想测试下“rails 中 preload、includes、Eager load、Joins 的区别”,想看看对应的 sql 语句,就是想要达到 rails s 的效果(有 sql、有配色)。

在网上找了资料,直接在 rails console 里输入 ActiveRecord::Base.logger = Logger.new(STDOUT),能直接在 iterm 中看到 sql 了,虽然没有配色,而且重新 rails c 还要执行这条命令。

但是在配置文件 config/environments/development.rb 下加入 ActiveRecord::Base.logger = Logger.new(STDOUT),重新执行 bundle exec rails c,却不行。

有些苦恼,求助下。😢 😢 😢

在 development.rb 中加入

config.after_initialize do
    logger = Logger.new(STDOUT)
    ActiveRecord::Base.logger = logger
    ActiveResource::Base.logger = logger
end

噢噢,没有写回调😅 那怎么给 sql 加配色呢

Reply to chromer

pry 是可以,,但是想在 rails c 上看,,

railsboy closed this topic. 28 Sep 16:09
railsboy reopened this topic. 02 Mar 13:00

作者你好,看了你写的文章觉得 666,能否加个 qq 聊聊?

railsboy closed this topic. 23 Apr 16:59
You need to Sign in before reply, if you don't have an account, please Sign up first.