4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

sessionにいろいろぶち込みすぎてエラーになる場合の対応

Posted at

sessionに大きなデータを突っ込もうとしたらエラーが出たので下記のように対応した。

(1)sessionの格納場所はcokie→DBに変更
activerecord-session_storeのgemを利用。
https://github.com/rails/activerecord-session_store

<Gemfile>
gem 'activerecord-session_store'

(1)-2 active_record:sessionのmigrate

rails generate active_record:session_migration
rake db:migrate

(1)-3 session用のテーブルを作成(→これはmigrateで自動作成される?かな)

<symptom-interview/db/migrate/20170201080607_add_sessions_table.rb>
class AddSessionsTable < ActiveRecord::Migration
  def change
    create_table :sessions do |t|
      t.string :session_id, :null => false
      t.text :data
      t.timestamps
    end

    add_index :sessions, :session_id, :unique => true
    add_index :sessions, :updated_at
  end

(2)controller 側は普通にセッションとして格納

<controller>
diagnosis_lists = DispDisease.all #とにかくなにか大きなデータ
session[:sheet]["diagnosis_lists"] = diagnosis_lists

(3)session_storeの場所の変更

<config/initializers/session_store.rb>

#従来こうなっている?はず
#Rails.application.config.session_store :cookie_store, key: '_symptom-interview_session'

#このように変更する。
Rails.application.config.session_store :active_record_store, key: '_symptom-interview_session'

多分これで出来るはず。

4
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
4
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?