Mysql `描述表格“在每个Rails请求中显示”

Mysql `描述表格“在每个Rails请求中显示”,mysql,ruby-on-rails,associations,Mysql,Ruby On Rails,Associations,我有两个模型,band和genre,以及通过关联表bands\u genres(没有模型)按以下方式建立的多对多关系 class Genre < ActiveRecord::Base has_and_belongs_to_many :bands class Band < ActiveRecord::Base has_and_belongs_to_many :genres 为什么会这样?如何以某种方式缓存此查询的结果以避免对每个请求执行此操作?在生产模式下运行服务器。在

我有两个模型,
band
genre
,以及通过关联表
bands\u genres
(没有模型)按以下方式建立的多对多关系

class Genre < ActiveRecord::Base
   has_and_belongs_to_many :bands

class Band < ActiveRecord::Base
   has_and_belongs_to_many :genres

为什么会这样?如何以某种方式缓存此查询的结果以避免对每个请求执行此操作?

在生产模式下运行服务器。在开发模式下,每个请求都会重新加载表信息

rails s -e production

在生产模式下运行服务器。在开发模式下,每个请求都会重新加载表信息

rails s -e production

这是因为您当前的环境配置告诉Rails这样做。我假设您使用“开发”,但您也有“生产”和“测试”

有一个选项可以在任何环境配置中缓存类。检查当前的一个(我假设您使用“开发”):

并将此选项修改为
true

    # In the development environment your application's code is reloaded on
    # every request.  This slows down response time but is perfect for development
    # since you don't have to restart the webserver when you make code changes.
    config.cache_classes = true
然后,使用当前环境运行服务器。促进发展:

    config/environments/development.rb
bundle exec rails s
够了

这有两件事:

    1) when Rails start, it will now read all classes' definitions
    (models) up front, and keep it for each request. 
    When you change a class now, no code will be reloaded automatically

    2) Rails will not ask database for model metadata change, 
    so no "describe table" will go to database in any request

默认情况下,“生产”环境将此选项设置为“真”。但“生产”环境是指生产,而不是发展。您可以在那里指定不同的选项、URL和变量

这是因为您当前的环境配置告诉Rails这样做。我假设您使用“开发”,但您也有“生产”和“测试”

有一个选项可以在任何环境配置中缓存类。检查当前的一个(我假设您使用“开发”):

并将此选项修改为
true

    # In the development environment your application's code is reloaded on
    # every request.  This slows down response time but is perfect for development
    # since you don't have to restart the webserver when you make code changes.
    config.cache_classes = true
然后,使用当前环境运行服务器。促进发展:

    config/environments/development.rb
bundle exec rails s
够了

这有两件事:

    1) when Rails start, it will now read all classes' definitions
    (models) up front, and keep it for each request. 
    When you change a class now, no code will be reloaded automatically

    2) Rails will not ask database for model metadata change, 
    so no "describe table" will go to database in any request

默认情况下,“生产”环境将此选项设置为“真”。但“生产”环境是指生产,而不是发展。您可以在那里指定不同的选项、URL和变量

酷,我以为在生产中也会有这种行为。谢谢我有疑问,因为我找不到任何证据来证明我的观点这仍然是Heroku上的一个问题,关于如何阻止这种情况发生有什么想法吗?酷,我认为这种行为在生产中也是一样的。谢谢我有疑问,因为我找不到任何证据来证明我的观点这仍然是Heroku的一个问题,关于如何阻止这种情况发生有什么想法吗?