本文介绍了Active Record 运行所有查询两次的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
不知道为什么在生产中会发生这种情况:
Have no idea why this is happening in production:
更新这是来自 Rails 控制台:
Update this is from the rails console:
User.all
User Load (0.5ms) SELECT "users".* FROM "users"
User Load (0.5ms) SELECT "users".* FROM "users"
2.0.0-p451 :005 > User.first.destroy
User Load (0.6ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
User Load (0.6ms) SELECT "users".* FROM "users" ORDER BY "users"."id" ASC LIMIT 1
(0.2ms) BEGIN
(0.2ms) BEGIN
Discussion Load (60.4ms) SELECT DISTINCT "discussions".* FROM "discussions" INNER JOIN "followers" ON "discussions"."id" = "followers"."discussion_id" WHERE "followers"."user_id" = $1 [["user_id", 1]]
Discussion Load (60.4ms) SELECT DISTINCT "discussions".* FROM "discussions" INNER JOIN "followers" ON "discussions"."id" = "followers"."discussion_id" WHERE "followers"."user_id" = $1 [["user_id", 1]]
Reply Load (0.8ms) SELECT "replies".* FROM "replies" WHERE "replies"."user_id" = $1 [["user_id", 1]]
Reply Load (0.8ms) SELECT "replies".* FROM "replies" WHERE "replies"."user_id" = $1 [["user_id", 1]]
SQL (0.4ms) DELETE FROM "users" WHERE "users"."id" = $1 [["id", 1]]
SQL (0.4ms) DELETE FROM "users" WHERE "users"."id" = $1 [["id", 1]]
(0.5ms) COMMIT
(0.5ms) COMMIT
我的制作设置:
config.cache_classes = false
config.eager_load = false
config.consider_all_requests_local = true
config.action_controller.perform_caching = false
config.action_mailer.default_url_options = { host: 'xxx' }
config.action_mailer.raise_delivery_errors = true
config.action_mailer.delivery_method = :smtp
config.action_mailer.smtp_settings = {
:authentication => :plain,
:address => "xxx",
:port => xxx,
:domain => "xxx",
:user_name => "xxx",
:password => "xxx"
}
config.active_support.deprecation = :log
config.active_record.migration_error = :page_load
config.serve_static_assets = :false
config.log_level = :debug
config.assets.debug = true
希望有一些常见的问题?真的很奇怪不是吗?
Hoping there's some common gotcha? Really odd isn't it?
推荐答案
我遇到了类似的问题,原来是我为 heroku 安装的 rails_12factor
gem 中的一个错误.只需在 Gemfile
I've had a similar problem, turned out it was a bug in rails_12factor
gem I installed for heroku. Just set it as a production gem in Gemfile
gem 'rails_12factor', group: :production
这篇关于Active Record 运行所有查询两次的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!