Rails 7.1.3配置:active_record会话存储报错求助
Rails 7.1.3 配置Active Record会话存储的正确方法
你遇到的undefined method 'new' for ActiveRecord:Module错误,核心原因是会话存储配置参数使用错误。
错误根源
activerecord-session_store gem向Rails注册的会话存储标识是:active_record_store,而非:active_record。当你配置:active_record时,Rails会错误地将ActiveRecord模块当作中间件类尝试实例化,但模块本身没有new方法,因此抛出异常。
解决步骤
- 修正初始化器配置
打开会话存储初始化器(通常为config/initializers/session_store.rb),将代码修改为:
Rails.application.config.session_store :active_record_store
- 生成会话表迁移文件
现在可以正常执行生成命令:
bundle exec rails generate active_record:session_migration
- 执行数据库迁移
创建会话存储所需的数据库表:
bundle exec rails db:migrate
- 验证配置生效
启动Rails控制台,确认会话存储配置正确加载:
Rails.application.config.session_store # 应返回 :active_record_store
额外注意事项
- 你Gemfile中使用了GitHub源的
activerecord-session_store,若无需开发版的最新修复,建议改用稳定版:
执行gem 'activerecord-session_store'bundle install更新依赖即可。
内容的提问来源于stack exchange,提问作者David
相关产品推荐
相关产品推荐

