0

As a new member of this community and moreover newer in Rails (version 5.1.1) i really need ur help.

Seems like my sessions - (stored in config/initializers/session_store.rb by gem as shown here Active_Record_Store gem and defined in config/application.rb as:

ActiveRecord::SessionStore::Session.table_name = 'legacy_session_table'
ActiveRecord::SessionStore::Session.primary_key = 'session_id'
ActiveRecord::SessionStore::Session.data_column_name = 'legacy_session_data' 

As shown here ActiveRecord::SessionStore - APIdock - don't initialize and I can't get why.

When i try to assign any value to any key in any controller (for example, application), it returns error:

undefined method []=' for nil:nilclass

Pry gem writes the same, then that the just session - is nil , and for request.session - <ActionDispatch::Request ::Session:0x7f92db05ae18 not yet loaded> . Session table is migrated, tried everything i can.

Please, help to tired noob initialize it and finally access the value of sessions and make them work! Thanks

Sebastián Palma
  • 32,692
  • 6
  • 40
  • 59

1 Answers1

3

From the rails documentation on the topic

"You may provide your own session class implementation, whether a feature-packed Active Record or a bare-metal high-performance SQL store, by setting"

ActiveRecord::SessionStore.session_class = MySessionClass

You must implement these methods:

self.find_by_session_id(session_id)
initialize(hash_of_session_id_and_data, options_hash = {})
attr_reader :session_id
attr_accessor :data
save
destroy

Alternatively I found this possible solution:

I found a way to force initialization of the session. Accessing the session apparently does not force initialization but writing into the session does. What I do in my controller is this now:

class MyController < ApplicationController
  protect_from_forgery

  def index
    session["init"] = true
    do_stuff
  end
end

Still I'm not sure if this should be considered normal behavior in Rails. It doesn't look right to me having to write into the session to force initialization. Reading should be enough.

Tinus Wagner
  • 927
  • 1
  • 7
  • 15