impressionist icon indicating copy to clipboard operation
impressionist copied to clipboard

Nil value for session_hash

Open kashifnaseer opened this issue 10 years ago • 9 comments

impressionist(@object, nil, unique: [:session_hash]) always gives NIL value for session_hash.

first time it inserts the record, later on every request it says Record Already Exist because it always find Nil value for session_hash. it looks it does not log the rails-4 session value into "session_hash"

Any help on this ? I'm using Rails 4.1.6, ruby 2.1.3p242 and impressionist (1.5.1)

kashifnaseer avatar Oct 30 '14 13:10 kashifnaseer

+1

khelal avatar Oct 30 '14 20:10 khelal

As per my little research, i see problem may be with def session_hash in ImpressionistController module. for rails-4 this needs to be fixed.

somehow impressionist is using request.session_options[:id] to get the session ID but in rails-4 it is giving a nil value.

Hence, impressionist with session_hash is not getting the correct SESSION ID and its failing.

kashifnaseer avatar Nov 05 '14 10:11 kashifnaseer

For someone else who is also facing this issue. temporarily overriding session_hash did the job. once we will get the proper fix in impressionist, we will remove this function.

def session_hash request.session.id end

it seems to be working fine.

kashifnaseer avatar Nov 05 '14 11:11 kashifnaseer

Hi @kashifnaseer I tried overriding the session_hash in my initializer like so

ImpressionistController::InstanceMethods.class_eval do 
  def session_hash
    request.session.id
  end
end

but I get <ActionDispatch::Request::Session:0x7fa2ce2e3650 not yet loaded> when trying to access the session. Any ideas why it isn't loaded yet? Where/How did you override the method?

Thanks!

germs12 avatar Nov 19 '14 15:11 germs12

...and just like that I found a solution. It's due to lazy loading of the session. Putting a session[:init] = true before accessing the session works. It now looks like this:

ImpressionistController::InstanceMethods.class_eval do 
  def session_hash
    session[:init] = true
    request.session.id
  end
end

Hope that helps!

germs12 avatar Nov 19 '14 15:11 germs12

:+1:

kashifnaseer avatar Nov 22 '14 07:11 kashifnaseer

@germs12 Where did you place that file and what did you name it? Thanks!

sergiotapia avatar Mar 19 '15 15:03 sergiotapia

@kashifnaseer Any ideas? http://stackoverflow.com/questions/29149659/using-impressionist-in-rails-4-1-is-not-logging-views-by-session-hash

sergiotapia avatar Mar 19 '15 16:03 sergiotapia

This is probably the cause of this bug. https://github.com/rails/rails/pull/19309/files

ghost avatar Nov 02 '20 10:11 ghost