You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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方法,因此抛出异常。

解决步骤

  1. 修正初始化器配置
    打开会话存储初始化器(通常为config/initializers/session_store.rb),将代码修改为:
Rails.application.config.session_store :active_record_store
  1. 生成会话表迁移文件
    现在可以正常执行生成命令:
bundle exec rails generate active_record:session_migration
  1. 执行数据库迁移
    创建会话存储所需的数据库表:
bundle exec rails db:migrate
  1. 验证配置生效
    启动Rails控制台,确认会话存储配置正确加载:
Rails.application.config.session_store
# 应返回 :active_record_store

额外注意事项

  • 你Gemfile中使用了GitHub源的activerecord-session_store,若无需开发版的最新修复,建议改用稳定版:
    gem 'activerecord-session_store'
    
    执行bundle install更新依赖即可。

内容的提问来源于stack exchange,提问作者David

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.25 06:28:18