Giter Site home page Giter Site logo

activerecord-session_store's Introduction

Active Record Session Store

A session store backed by an Active Record class. A default class is provided, but any object duck-typing to an Active Record Session class with text session_id and data attributes is sufficient.

Installation

Include this gem into your Gemfile:

gem 'activerecord-session_store'

Run the migration generator:

rails generate active_record:session_migration

Run the migration:

rake db:migrate

Then, set your session store in config/initializers/session_store.rb:

Rails.application.config.session_store :active_record_store, :key => '_my_app_session'

To avoid your sessions table expanding without limit as it will store expired and potentially sensitive session data, it is strongly recommended in production environments to schedule the db:sessions:trim rake task to run daily. Running bin/rake db:sessions:trim will delete all sessions that have not been updated in the last 30 days. The 30 days cutoff can be changed using the SESSION_DAYS_TRIM_THRESHOLD environment variable.

Configuration

The default assumes a sessions table with columns:

  • id (numeric primary key),
  • session_id (string, usually varchar; maximum length is 255), and
  • data (text, longtext, json or jsonb); careful if your session data exceeds 65KB).

The session_id column should always be indexed for speedy lookups. Session data is marshaled to the data column in Base64 format. If the data you write is larger than the column's size limit, ActionController::SessionOverflowError will be raised.

You may configure the table name, primary key, data column, and serializer type. For example, at the end of config/application.rb:

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

Note that setting the primary key to the session_id frees you from having a separate id column if you don't want it. However, you must set session.model.id = session.session_id by hand! A before filter on ApplicationController is a good place.

The serializer may be class responding to #load(value) and #dump(value), or a symbol of marshal, json, hybrid or null. marshal is the default and uses the built-in Marshal methods coupled with Base64 encoding. json does what it says on the tin, using the parse() and generate() methods of the JSON module. hybrid will read either type but write as JSON. null will not perform serialization, leaving that up to the ActiveRecord database adapter. This allows you to take advantage of the native JSON capabilities of your database.

Since the default class is a simple Active Record, you get timestamps for free if you add created_at and updated_at datetime columns to the sessions table, making periodic session expiration a snap.

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

ActionDispatch::Session::ActiveRecordStore.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

The example SqlBypass class is a generic SQL session store. You may use it as a basis for high-performance database-specific stores.

Please note that you will need to manually include the silencer module to your custom logger if you are using a logger other than ActiveSupport::Logger and its subclasses:

MyLogger.include ActiveSupport::LoggerSilence

Or if you are using Rails 5.2 or older:

MyLogger.include ::LoggerSilence

This silencer is being used to silence the logger and not leaking private information into the log, and it is required for security reason.

CVE-2019-25025 mitigation

Sessions that were created by Active Record Session Store version 1.x are affected by CVE-2019-25025. This means an attacker can perform a timing attack against the session IDs stored in the database.

After upgrade to version 2.0.0, you should run db:sessions:upgrade rake task to upgrade all existing session records in your database to the secured version.

$ rake db:sessions:upgrade

This rake task is idempotent and can be run multiple times, and session data of users will remain intact.

Please see #151 for more details.

Contributing to Active Record Session Store

Active Record Session Store is work of many contributors. You're encouraged to submit pull requests, propose features and discuss issues.

See CONTRIBUTING.

License

Active Record Session Store is released under the MIT License.

activerecord-session_store's People

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

activerecord-session_store's Issues

"Thread.exclusive is deprecated, use Mutex" error

When I use rake while using this gem, I get Thread.exclusive is deprecated, use Mutex error on this line:

.../.rvm/gems/ruby-2.3.0/gems/activerecord-session_store-0.1.2/lib/
active_record/session_store/session.rb:32:in `find_by_session_id'

followed by stacktrace.

Error on all requests

I receive the following error on all requests. This is related to upgrading to rails 4.

ArgumentError (wrong number of arguments calling capture (0 for 1)):
/home.rvm/gems/jruby-1.7.9/gems/activerecord-session_store-0.1.1/lib/action_dispatch/session/active_record_store.rb:65:in get_session' /home.rvm/gems/jruby-1.7.9/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:266:inload_session'
/home.rvm/gems/jruby-1.7.9/gems/actionpack-4.0.13/lib/action_dispatch/middleware/session/abstract_store.rb:43:in load_session' /home.rvm/gems/jruby-1.7.9/gems/actionpack-4.0.13/lib/action_dispatch/middleware/session/abstract_store.rb:51:instale_session_check!'
/home.rvm/gems/jruby-1.7.9/gems/actionpack-4.0.13/lib/action_dispatch/middleware/session/abstract_store.rb:43:in load_session' /home.rvm/gems/jruby-1.7.9/gems/actionpack-4.0.13/lib/action_dispatch/request/session.rb:168:inload!'
/home.rvm/gems/jruby-1.7.9/gems/actionpack-4.0.13/lib/action_dispatch/request/session.rb:160:in load_for_read!' /home.rvm/gems/jruby-1.7.9/gems/actionpack-4.0.13/lib/action_dispatch/request/session.rb:86:in[]'
/home.rvm/gems/jruby-1.7.9/gems/warden-1.2.3/lib/warden/session_serializer.rb:30:in fetch' /home.rvm/gems/jruby-1.7.9/gems/warden-1.2.3/lib/warden/proxy.rb:212:inuser'
/home.rvm/gems/jruby-1.7.9/gems/warden-1.2.3/lib/warden/proxy.rb:318:in _perform_authentication' /home.rvm/gems/jruby-1.7.9/gems/warden-1.2.3/lib/warden/proxy.rb:104:inauthenticate'
/home.rvm/gems/jruby-1.7.9/gems/warden-1.2.3/lib/warden/proxy.rb:114:in authenticate?' /home.rvm/gems/jruby-1.7.9/gems/devise-3.4.1/lib/devise/controllers/sign_in_out.rb:10:insigned_in?'
org/jruby/RubyEnumerable.java:1474:in `any?'

ArgumentError: string contains null byte when using postgresql and session_id is set to %00

bundle list
Gems included by the bundle:
  * actionmailer (4.2.4)
  * actionpack (4.2.4)
  * actionview (4.2.4)
  * activejob (4.2.4)
  * activemodel (4.2.4)
  * activerecord (4.2.4)
  * activerecord-session_store (1.0.0)
  * activesupport (4.2.4)
  * arel (6.0.3)
  * builder (3.2.2)
  * bundler (1.10.6)
  * concurrent-ruby (1.0.2)
  * erubis (2.7.0)
  * globalid (0.3.6)
  * i18n (0.7.0)
  * json (1.8.3)
  * loofah (2.0.3)
  * mail (2.6.4)
  * mime-types (3.1)
  * mime-types-data (3.2016.0521)
  * mini_portile2 (2.0.0)
  * minitest (5.9.0)
  * multi_json (1.12.1)
  * nokogiri (1.6.7.2)
  * pg (0.18.4)
  * rack (1.6.4)
  * rack-test (0.6.3)
  * rails (4.2.4)
  * rails-deprecated_sanitizer (1.0.3)
  * rails-dom-testing (1.0.7)
  * rails-html-sanitizer (1.0.3)
  * railties (4.2.4)
  * rake (11.1.2)
  * sprockets (3.6.0)
  * sprockets-rails (3.0.4)
  * thor (0.19.1)
  * thread_safe (0.3.5)
  * tzinfo (1.2.2)

config/database.yml

development:
  adapter: postgresql
  database: ****
  encoding: unicode
  template: template0
  pool: 1
  host: localhost
  username: ****

config/initializer/session_store.rb

Rails.application.config.session_store :active_record_store, key: '_session_id'

app/controller/application_controller.rb

  def index
    session[:hoge] = 'foo bar'
  end

With above setting,
curl --verbose --cookie '_session_id=%00' http://localhost:3000/
yields 500 Internal Server Error.

SELECT  "sessions".* FROM "sessions" WHERE "sessions"."session_id" = $1  ORDER BY "sessions"."id" ASC LIMIT 1  [["session_id", "\u0000"]]
ArgumentError: string contains null byte: SELECT  "sessions".* FROM "sessions" WHERE "sessions"."session_id" = $1  ORDER BY "sessions"."id" ASC LIMIT 1
Completed 500 Internal Server Error in 1ms (ActiveRecord: 0.2ms)

ArgumentError (string contains null byte):
  app/controllers/application_controller.rb:7:in `index'

comparison of Fixnum with nil failed

Hi,

I am getting this error "Unexpected error while processing request: comparison of Fixnum with nil faile"
when I try to do any actions like rake db:create, rake db:migrate and rails s in my mac os yosemite. This is the log

 hemas-MacBook-Pro:inotary hemashree$ rake db:migrate
rake aborted!
ArgumentError: comparison of Fixnum with nil failed
/Users/hemashree/.bundler/ruby/2.0.0/activerecord-session_store-2d9517d0f262/lib/active_record/session_store/extension/logger_silencer.rb:34:in `<'
/Users/hemashree/.bundler/ruby/2.0.0/activerecord-session_store-2d9517d0f262/lib/active_record/session_store/extension/logger_silencer.rb:34:in `add_with_threadsafety'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:438:in `rescue in log'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract_adapter.rb:435:in `log'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/postgresql/database_statements.rb:137:in `exec_query'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/postgresql_adapter.rb:891:in `select'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract/database_statements.rb:24:in `select_all'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/connection_adapters/abstract/query_cache.rb:63:in `select_all'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/querying.rb:36:in `find_by_sql'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/relation.rb:585:in `exec_queries'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/relation.rb:471:in `load'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/relation.rb:220:in `to_a'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/relation/delegation.rb:12:in `map'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/migration.rb:787:in `get_all_versions'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/migration.rb:949:in `migrated'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/migration.rb:954:in `ran?'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/migration.rb:931:in `block in runnable'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/migration.rb:931:in `reject'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/migration.rb:931:in `runnable'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/migration.rb:908:in `migrate'
/Library/Ruby/Gems/2.0.0/gems/activerecord-4.0.1/lib/active_record/migration.rb:764:in `up'
/Library/Ruby/Gems/2.0.0/gems/activer

please find this and give me the solution.

Session isn't reset after logging out

In my app, the user can login via oauth (using omniauth). They log out by calling Devise::SessionsController#destroy. When this happens, current_user is nil, and the session object is reset. All good, seemingly.

However, when the user tries to login via oauth again, and the omniauth callback gets called, current_user is already present! In the callback, session.to_json returns a valid session with the user that was previously logged in. This makes me think that the session isn't resetting properly during logout. I've spent a fair amount of time trying to debug this line by line but haven't gotten anywhere. Has anyone else experienced this problem?

I read over this issue but that was for cookie-store only.

Improve schema of auto generated sessions table

Imo the migration generated by rake db:sessions:create should be improved.

As it's now:

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
    add_index :sessions, :updated_at
  end
end

My suggestion:

class AddSessionsTable < ActiveRecord::Migration
  def change
    create_table :sessions do |t|
      t.string :session_id, :null => false
      t.text :data, :null => false
      t.timestamps :null => false
    end

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

Also see: rails/rails#7453

Bundler dependency problem with Rails 4-0-stable

I was seeing the following error when trying to use Rails 4-0-stable branch (from Github)
in the Gemfile of my Rails app:

Bundler could not find compatible versions for gem "actionpack":
  In Gemfile:
    activerecord-session_store (>= 0) ruby depends on
      actionpack (= 4.0.0) ruby

    activerecord-session_store (>= 0) ruby depends on
      actionpack (4.0.0.rc2)

I came up with this fix, but I don't think it's the right solution, since it means people can't use activerecord-session_store with beta or release-candidate versions of Rails v4.0.0.

Does anyone have any better ideas?

Could not find generator active_record:session_migration

I try install this gem in Windows, but doesn't work.
I'm using:

  • Rails 4.0 (Rails Installer)
  • Ruby 2.0 (Ruby Installer)
  • Bundler 1.3.5
  • Windows 7 x64 Professional

Gemfile (last line)

gem 'activerecord-session_store', github: 'rails/activerecord-session_store'

Commands (in order)

  • gem install activerecord-session_store
  • bundle install (not list activerecord-session_store)

Error

When I run this command:

  • rails generate active_record:session_migration

Return this:

  • Could not find generator active_record:session_migration.

I don't know whats happen.


Thanks, and sorry for my english.

table_name_prefix duplicated by active record session migration

When the config.active_record.table_name_prefix variable is set, the

rails generate active_record:session_migration

creates a migration with the prefixed table name in the class. When this migration is run, the prefix is applied again. Upon changing the session_store to active record, and assuming a table_name_prefix = "prefix_", an error will be thrown:

Could not find table 'prefix_sessions'

A work around this is modifying the migration before it is run and removing the prefix. However, it seems that the migration should be created without the prefix, as it will be applied when run.

Reproduced on a new rails 3.2.13 app.

Rails 5/rack 2 support

I tried using activerecord-session_store with a fresh Rails 5 app today. Using the generator I hit:

uninitialized constant Rack::Session::Abstract::ENV_SESSION_OPTIONS_KEY

I assume this is because Rails 5 now depends on rack (~> 2.x) and this is no longer the correct constant.

assets:precompile tries to establish a database connection

When this gem is in the Gemfile, it is included whenever the rails environment is loaded since it is in the default group. Since it touches the db on load, it breaks assets:precompile when no database is configured.

A workaround is to use

gem 'activerecord-session_store', require: false

in the Gemfile and then

require 'activerecord/session_store'

in app/controllsers/application_controller.rb. Also, it took me a lot of time to determine which gem was responsible for the db connection. This is how I ended up "debugging" it: I edited active_record/lib/active_record/base.rb and added "puts caller" to the top of the file. The stacktrace would then show which gem was loading it.

Version 0.1.1 breaks Syslog::Logger setups

For those of us that use Syslog::Logger for our config.logger (config/environments/[env].rb) are having an issue with the latest update. Mainly, instances of Syslog::Logger do not respond to silence. However, they do respond to quietly method. Here is the error it creates:

helix (webdev) > ActiveRecord::Base.logger.silence
ArgumentError: wrong number of arguments (0 for 1)
from /usr/local/lib/ruby/gems/2.0.0/gems/activesupport-4.1.9/lib/active_support/core_ext/kernel/reporting.rb:82:in `capture'
from (irb):3
from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.1.9/lib/rails/commands/console.rb:90:in `start'
from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.1.9/lib/rails/commands/console.rb:9:in `start'
from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:69:in `console'
from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.1.9/lib/rails/commands/commands_tasks.rb:40:in `run_command!'
from /usr/local/lib/ruby/gems/2.0.0/gems/railties-4.1.9/lib/rails/commands.rb:17:in `<top (required)>'
from /home/helix/app/current/bin/rails:4:in `require'
from /home/helix/app/current/bin/rails:4:in `<main>'
helix (webdev) > ActiveRecord::Base.logger.class
=> Syslog::Logger
helix (webdev) > 

logger is set installer here which in our case is ActiveRecord::Base.logger.

There's no changelog

Hi there!

I currently have this gem locked at '~> 0.1' in my Gemfile.

I see the gem has transitioned to 1.0. Is it safe to update my dependency?

(as per semver, theoretically anything could have changed from 0.x to 1!)

Maintaining a changelog would greatly help.

Cheers - Victor

Potential issue with silencer and LogstashLogger/Sidekiq?

Hello,
Using activerecord-session_store-1.0.0 with Rails 4.2.5, ruby 2.2.3p173, and sidekiq v4.1.1 We've recently updated this project from Rails 3.2 and this Rails 4 version has not yet been deployed to production.

We have noticed what appears to be almost daily hanging of all of our sidekiq processes. When we investigate the logs, the commonality is that the last thing each hung process did was log something simple. Looking at the backtrace we see this pattern:

2016-06-17T20:23:47.574Z 18071 TID-gqnfahehc WARN: Thread TID-gqnfqn8gc 
2016-06-17T20:23:47.574Z 18071 TID-gqnfahehc WARN: /opt/elasticbeanstalk/lib/ruby/lib/ruby/2.2.0/monitor.rb:185:in `lock'
/opt/elasticbeanstalk/lib/ruby/lib/ruby/2.2.0/monitor.rb:185:in `mon_enter'
/opt/elasticbeanstalk/lib/ruby/lib/ruby/2.2.0/monitor.rb:209:in `mon_synchronize'
/opt/elasticbeanstalk/lib/ruby/lib/ruby/2.2.0/logger.rb:595:in `write'
/opt/elasticbeanstalk/lib/ruby/lib/ruby/2.2.0/logger.rb:378:in `add'
/var/app/current/vendor/bundle/ruby/2.2.0/gems/activerecord-session_store-1.0.0/lib/active_record/session_store/extension/logger_silencer.rb:38:in `add_with_threadsafety'
/opt/elasticbeanstalk/lib/ruby/lib/ruby/2.2.0/logger.rb:434:in `info'
/var/app/current/app/workers/parking_session_parking_picture.rb:33:in `process!'

Where the last thing our application did was a simple Rails.logger.info statement. This is happening in many different classes, but they always have this pattern as their last actions:

2016-06-17T20:23:47.572Z 18071 TID-gqnfahehc WARN: Thread TID-gqngzan1k 
2016-06-17T20:23:47.572Z 18071 TID-gqnfahehc WARN: /opt/elasticbeanstalk/lib/ruby/lib/ruby/2.2.0/monitor.rb:185:in `lock'
/opt/elasticbeanstalk/lib/ruby/lib/ruby/2.2.0/monitor.rb:185:in `mon_enter'
/opt/elasticbeanstalk/lib/ruby/lib/ruby/2.2.0/monitor.rb:209:in `mon_synchronize'
/opt/elasticbeanstalk/lib/ruby/lib/ruby/2.2.0/logger.rb:595:in `write'
/opt/elasticbeanstalk/lib/ruby/lib/ruby/2.2.0/logger.rb:378:in `add'
/var/app/current/vendor/bundle/ruby/2.2.0/gems/activerecord-session_store-1.0.0/lib/active_record/session_store/extension/logger_silencer.rb:38:in `add_with_threadsafety'
/opt/elasticbeanstalk/lib/ruby/lib/ruby/2.2.0/logger.rb:434:in `info'

To further complicate matters, we are using LogstashLogger. I believe we have properly configured the silencer (LogStashLogger is a Module not a class):

  # Manually Including Silencer is required by ActiveRecord::SessionStore
  config.logger.extend ActiveRecord::SessionStore::Extension::LoggerSilencer

Any recommendations on further troubleshooting? Not even sure this is an activerecord-session_store issue, but it seemed like a logical place to start.

DEPRECATION WARNING: `#quietly` is deprecated in rails-4.2.0.beta4

Getting this while in 4.2.0.beta4.

DEPRECATION WARNING: `#quietly` is deprecated and will be removed in the next release.
(called from get_session at /Users/Austris/.rvm/gems/ruby-2.1.5@rails-pre/bundler/gems/activerecord-session_store-e879bf087eb6/lib/action_dispatch/session/active_record_store.rb:64)

Support ruby 2.3

Deprecation error Thread.exclusive is deprecated, use Mutex on lib/active_record/session_store/session.rb:32

ActiveRecord::RecordNotFound Couldn't find all Sessions with 'id'

I have an error in rails 4.1.4 with the details in the log below but rails 3.2.13 I didn't encounter this error. Same configuration.

When I didn't set my custom class as the session class for ActiveRecordStore, it works fine.

# app/models/session.rb
class Session < ActiveRecord::SessionStore::Session
  belongs_to :user
  before_save :ensure_user_is_set

  def self.find_by_session_id(session_id)
    find :first, :conditions => {:session_id=>session_id}
  end

  private
  def ensure_user_is_set
    warden_data = self.data['warden.user.user.key']
    if warden_data
      self.user = User.find(warden_data[0][0])
    end
  end
end
# config/initializers/session_store.rb
Cms::Application.config.session_store :active_record_store, {
    key: 'SESSION',
    expire_after: 24.hours
}

ActionDispatch::Session::ActiveRecordStore.session_class = Session

Logs:

ActiveRecord::RecordNotFound (Couldn't find all Sessions with 'id': (first, {:conditions=>{:session_id=>"504f4919e328d77863d03a7be6a6119e"}}) (found 0 results, but was looking for 2)):
  app/models/session.rb:6:in `find_by_session_id'


  Rendered /Users/itsmechlark/.rvm/gems/ruby-2.1.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_source.erb (0.9ms)
  Rendered /Users/itsmechlark/.rvm/gems/ruby-2.1.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_trace.html.erb (1.3ms)
  Rendered /Users/itsmechlark/.rvm/gems/ruby-2.1.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/_request_and_response.html.erb (6.5ms)
  Rendered /Users/itsmechlark/.rvm/gems/ruby-2.1.0/gems/actionpack-4.1.4/lib/action_dispatch/middleware/templates/rescues/diagnostics.erb within rescues/layout (33.9ms)

rails v4.1.4

Secure flag set?

I can set expire_after, but can I set the secure flag like below?

App::Application.config.session_store :active_record_store, {
:expire_after => 10.hours,
:secure => true }

Rails 5 before_create/before_save callbacks not working

I'm updating an app from Rails 4.2 -> Rails 5 and running into a problem around sessions.

I have some before_actions in controllers that add data to the session, like session[:foo] = 'value'.

Then in my session model that inherits from ActiveRecord::SessionStore::Session, I have a before_create hook that pulls the data out of the serialized data string and stores it in its own field on the model so I can query by this field.

After updating to Rails 5, this before_save hook is no longer being triggered. I've tried adding a bunch of other hooks like before_save, after_save and after_commit and none of these are getting triggered when activerecord sets the serialized data field. When I turn on ActiveRecord debug logging, I can see the UPDATE "sessions" ... statement that runs which sets the session data correctly.

How can I hook into this UPDATE call?

null value in column "session_id" violates not-null constraint

Just switched to this gem, using Rails 4 beta. When creating a new session I am getting this error. From my specs:

  1) FacebookAuths creates a user after authenticating with facebook
     Failure/Error: click_link "Sign in with Facebook"
     ActiveRecord::StatementInvalid:
       PG::Error: ERROR:  null value in column "session_id" violates not-null constraint
       DETAIL:  Failing row contains (1, null, BAh7B0kiFG9tbmlhdXRoLnBhcmFtcwY6BkVGewBJIhRvbW5pYXV0aC5vcmln
       aW4..., 2013-01-07 22:08:28.269912, 2013-01-07 22:08:28.269912).
       : INSERT INTO "sessions" ("created_at", "data", "updated_at") VALUES ($1, $2, $3) RETURNING "id"
     # ./spec/features/facebook_auths_spec.rb:7:in `block (2 levels) in <top (required)>'

Not sure what I am doing wrong. It had been working fine with the cookie store, but I got the Cookie Overflow error when I switched to the encrypted cookie store, which is how I ended up here. I am not sure if there is something else I need to change.

Session does not get saved if the session data has not been loaded

Extracted from my recent PR as it is already merged:

Since Rails 5, we're having the problem though that the session doesn't get saved unless its data has been loaded. We're extending the built-in session model class and have added additional fields which we'd like to update.

For instance

class CustomSession < ActiveRecord::SessionStore::Session
  def set_updated_at!
    our_custom_session.update_attributes!(updated_at: Time.now)
  end
end

throws a ActiveRecord::RecordNotSaved exception, since it didn't load the session data (as it doesn't care for it at this place). Our current workaround for this is to trigger loading the data as such:

  def set_updated_at!
    data
    our_custom_session.update_attributes!(updated_at: Time.now)
  end

But this is most certainly not a clean way of doing it. Is it really the expected behavior to always halt the saving chain when the data hasn't been loaded? And I also can't figure why this didn't happen in Rails 4.

Thanks a lot for your help.

How can I clear sessions from the db immediately on logout?

I'm having an issue where if I create a new account, login using Devise and then logout, it looks as though the user has logged out. But if I return to a page, say the dashboard, it will let me see it even though I've logged out and the dashboard requires authentication to see. Even more disturbing is when I log out and then create another account, the new user is directed to the dashboard of the first user that just logged out (Devise is setup to automatically direct the new user to their dashboard but instead they are directed to the dashboard of the previous user). So a new user could see someone else's account data.

From what I understand, this is a simple issue with the session still being active. I installed your gem a while ago and it cleared up another issue I was having but from my understanding this issue will remain for any session store unless I setup something to expire the sessions.

I just added Devise's :timeoutable to my user model but I don't think that will take care of the issue when someone logs out. I think it will only expire the session after the time I set.

I'm new to this so I'm not sure how to expire a session in the database immediately upon logout.

Any suggestions?

Intermittent success with Devise sign in after installing activerecord-session_store in rails 4.2.6

I installed activerecord-session_store the other day and all seemed well. Deployed to production, and fixed an issue I was having with something else. Only issue now is that ~30% of the time sign in with devise isn't working correctly. The sign in appears to authenticate and redirect to the correct user page, but current_user is gone... So, assuming this has to do with using activerecord-session_store.

Using

activerecord-session_store (1.0.0)
devise (4.1.1)

get_session is not thread-safe

This code is not thread-safe:

  def get_session(env, sid)
      ActiveRecord::Base.logger.quietly do

because ActiveRecord::Base.logger.quietly is not thread-safe.

See rails/rails#11954

The resulting issue is that this can break stdout in a multi-threaded process.

The real-world issue we're hitting is that after upgrading to rails 4, when we run our specs the standard output from rspec disappears, and the test results are not visible.

This happens when there are multiple requests hitting rack, which spawns a thread for each one. These threads in turn call 'get_session' concurrently, and the stdout is being improperly "restored" to '/dev/null' due to the thread unsafety here.

Not sure if this should be fixed here or possibly in active_support (again, rails/rails#11954).

Upgrading to 0.1.1 fails (ArgumentError)

As part of #36, I have been trying to upgrade from activerecord-session_store 0.0.1 -> 0.1.1, but I am having issues. In particular, I'm seeing the following kind of errors:

{ "message": "Backtrace: [\"/Users/ssteeg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activesupport-4.2.3/lib/active_support/core_ext/kernel/reporting.rb:89:in `capture'\", 
\"/Users/ssteeg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/activerecord-session_store-0.1.1/lib/action_dispatch/session/active_record_store.rb:65:in `get_session'\", \"/Users/ssteeg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/rack-1.6.4/lib/rack/session/abstract/id.rb:266:in `load_session'\", 
\"/Users/ssteeg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/session/abstract_store.rb:43:in `block in load_session'\", 
\"/Users/ssteeg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/session/abstract_store.rb:51:in `stale_session_check!'\", 
\"/Users/ssteeg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/middleware/session/abstract_store.rb:43:in `load_session'\", 
\"/Users/ssteeg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/request/session.rb:180:in `load!'\", 
\"/Users/ssteeg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/request/session.rb:172:in `load_for_read!'\", 
\"/Users/ssteeg/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0/gems/actionpack-4.2.3/lib/action_dispatch/request/session.rb:89:in `[]'\", 
\"/Users/ssteeg/work/hercules/app/controllers/application_controller.rb:43:in `set_current_user'\",
...
{ "message": "The following uncaught exception occurred: #<ArgumentError: wrong number of arguments (0 for 1)>" }

The code referenced isn't doing anything strange, just something like:

def set_current_user
  ...
  current_user.login = session[:cas_user]
  ...
end

Have you seen this kind of error before? Are there any gotcha's when upgrading to the latest version of this gem?

How could I silence the logs in rails server?

It's a bit hard to debug with SQL statements like these taking up most of the console.

SQL (1.9ms)  UPDATE "sessions" SET "data" = $1, "updated_at" = $2 WHERE "sessions"."id" = $3  [["data", "BAh7DkkiEXByZXZpb3VzX3VybAY6BkVGSSIVL215X3Byb2ZpbGUvZWRpdAY7\nAFRJIhhhcHBsaWNhbnRfcmV........

RuntimeError: rescue in session_store

I am in the process of upgrading my app from rails v 3.2.15 to 4.0.4. The app is crashing throwing the following error even though I am having activerecord-session_store gem in the Gemfile:

/home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/application/configuration.rb:140:in `rescue in session_store': `ActiveRecord::SessionStore` is extracted out of Rails into a gem. Please add `activerecord-session_store` to your Gemfile to use it. (RuntimeError)
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/application/configuration.rb:137:in `session_store'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/application.rb:345:in `block in default_middleware_stack'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/application.rb:309:in `tap'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/application.rb:309:in `default_middleware_stack'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/engine.rb:494:in `app'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/application/finisher.rb:34:in `block in <module:Finisher>'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/initializable.rb:30:in `instance_exec'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/initializable.rb:30:in `run'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/initializable.rb:55:in `block in run_initializers'
  from /home/aslam/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:226:in `block in tsort_each'
  from /home/aslam/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:348:in `block (2 levels) in each_strongly_connected_component'
  from /home/aslam/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:427:in `each_strongly_connected_component_from'
  from /home/aslam/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:347:in `block in each_strongly_connected_component'
  from /home/aslam/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `each'
  from /home/aslam/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `call'
  from /home/aslam/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:345:in `each_strongly_connected_component'
  from /home/aslam/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:224:in `tsort_each'
  from /home/aslam/.rvm/rubies/ruby-2.1.0/lib/ruby/2.1.0/tsort.rb:205:in `tsort_each'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/initializable.rb:54:in `run_initializers'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/application.rb:215:in `initialize!'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/railtie/configurable.rb:30:in `method_missing'
  from /home/aslam/work/projects/awesome/config/environment.rb:5:in `<top (required)>'
  from /home/aslam/work/projects/awesome/config.ru:3:in `require'
  from /home/aslam/work/projects/awesome/config.ru:3:in `block in <main>'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/rack-1.5.2/lib/rack/builder.rb:55:in `instance_eval'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/rack-1.5.2/lib/rack/builder.rb:55:in `initialize'
  from /home/aslam/work/projects/awesome/config.ru:in `new'
  from /home/aslam/work/projects/awesome/config.ru:in `<main>'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/rack-1.5.2/lib/rack/builder.rb:49:in `eval'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/rack-1.5.2/lib/rack/builder.rb:49:in `new_from_string'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/rack-1.5.2/lib/rack/builder.rb:40:in `parse_file'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/rack-1.5.2/lib/rack/server.rb:277:in `build_app_and_options_from_config'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/rack-1.5.2/lib/rack/server.rb:199:in `app'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/commands/server.rb:48:in `app'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/rack-1.5.2/lib/rack/server.rb:314:in `wrapped_app'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/commands/server.rb:75:in `start'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/commands.rb:76:in `block in <top (required)>'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/commands.rb:71:in `tap'
  from /home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/railties-4.0.4/lib/rails/commands.rb:71:in `<top (required)>'
  from bin/rails:4:in `require'
  from bin/rails:4:in `<main>'

I am using Bundler 1.5.3

$> bundle show activerecord-session_store
/home/aslam/.rvm/gems/ruby-2.1.0@awesome/gems/activerecord-session_store-0.1.0

Drops validation from belongs_to relations in Rails 5

In Rails 5, a belongs_to relation is automatically validated as required. Adding activerecord-session_store to the Gemfile of a Rails 5 project removes this automatic validation.

This can be reproduced very easily by creating a Rails 5 project with two related models:

rails new quuz
cd quuz
rails g model foo name:string
rails g model bar name:string foo:references

In the rails console or a test, it can now easily be verified that a Bar object is invalid without a Foo object:

Bar.new.valid?                # => false
Bar.new(foo: Foo.new).valid?  # => true

If activerecord-session_store is added to the Gemfile like so,

gem 'activerecord-session_store'

the results are different:

Bar.new.valid?                # => true
Bar.new(foo: Foo.new).valid?  # => true

errors when trying to migrate the sessions table that seem to relate to the schema prefix

I have an application that uses a schema prefix of eo.

When I installed the gem, ran the generator for session migrate and then tried to do a db:migrate, I got

rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

undefined method `sessions' for :eo:Symbol
C:/Users/cmendla/RubymineProjects/employee_observations/db/migrate/20160613152644_add_sessions_table.rb:3:in `change'
C:in `migrate'
-e:1:in `load'
-e:1:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)
== 20160613152644 AddSessionsTable: migrating =================================

Since it seemed to be having trouble with the prefix, I changed the migration to eliminate the prefixes.

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
end

However, I still am getting an error of

C:\Ruby200\bin\ruby.exe -e $stdout.sync=true;$stderr.sync=true;load($0=ARGV.shift) C:/Users/cmendla/RubymineProjects/employee_observations/bin/rake db:migrate
== 20160613152644 AddSessionsTable: migrating =================================
-- create_table(:sessions)
   -> 0.0050s
   -> -1 rows
rake aborted!
StandardError: An error has occurred, this and all later migrations canceled:

undefined method `sessions' for :eo:Symbol
C:/Users/cmendla/RubymineProjects/employee_observations/db/migrate/20160613152644_add_sessions_table.rb:9:in `change'
C:in `migrate'
-e:1:in `load'
-e:1:in `<main>'
Tasks: TOP => db:migrate
(See full trace by running task with --trace)

Process finished with exit code 1

I believe that this is related to the fact that I am using a schema prefix.

uninitialized constant ActiveRecord::SessionStore in Rails 4.2.0 engine

We are upgrading a Rails 3.2 engine to Rails 4.2.0 on ruby 2.0.0. Gem 'activerecord-session_store' was added to engine's gemspec following gem's instruction:

s.add_dependency 'activerecord-session_store'

and added following in initializers/session_store.rb under dummy:

Dummy::Application.config.session_store :active_record_store, :key => '_my_app_session'

then, we did bundle install. When we ran:

bundle exec rails generate active_record:session_migration

There is the error from the gem's generator:

/activerecord-session_store-0.1.1/lib/generators/active_record/session_migration_generator.rb:16:in `session_table_name': uninitialized co
nstant ActiveRecord::SessionStore (NameError).

We moved the gem into engine's Gemfile and there is same error. Is this a compatibility issue or something we missed?

Known issue: Does not currently working on edge Rails (5.x.x)

Latest Rails will include a new version of Rack which contains code that is not straightforward backward compatible. If you are relying on activerecord-session_store for your project, please consider submitting a patch to make it work.

activerecord-session_store currently uses Appraisals to help testing against multiple version of Rails. If you would like to contribute, here's the tl;dr steps:

$ git clone [email protected]:rails/activerecord-session_store.git
$ appraisal update

# run the test against all version
$ appraisal rake

# run the test only against edge rails
$appraisal edge rake

We're looking forward to see your patch. Thank you for your help!

Question: is the gem considered deprecated?

Notes like this and this (quote: "Rails 4 has deprecated many things and moved them into separate gems.") lead me to think this is meant to be deprecated and may not necessarily support 4.1+.

I was a bit confused on this point since I could not find any information about support/deprecation in the gem itself. I could have expected something more along the lines of the Rails finder deprecation.

Comments like this lead me to think I may not be the only one confused.

Can someone who knows clarify the planned future for this gem a bit and whether it will/is considered deprecated? @sikachu @rafaelfranca

devise 4.0.0.rc1 sign_in does not work with current master (rails 5.0.0.beta2 used)

Hi!

I'm not sure actually it is activerecord-session_store issue or devise issue.
But sign_in function does not work with devise 4.0.0.rc1, rails 5.0.0.beta2 and activerecord-session_store master.

I did some investigations and have fix for that.
There is one line needs to be fixed.
File: lib/action_dispatch/session/active_record_store.rb
Function: get_session_model
Line: id = generate_sid
Change to: id ||= generate_sid

Steps to reproduce described issue:

  1. Create new Rails app with rails 5.0.0.beta2
  2. add these lines to Gemfile:
    gem 'devise', '4.0.0.rc1'
    gem 'activerecord-session_store', :git => 'https://github.com/rails/activerecord-session_store.git'
  3. Run these commands:
    bundle install
    rails generate devise:install
    rails generate devise user
    rake db:migrate
  4. Add to config/environments/development.rb
    config.action_mailer.default_url_options = { host: 'localhost', port: 3000 }
  5. point root in routes to somewhere (devise will not start without it)
    root to: "home#index"
  6. Run this:
    rails generate controller home
  7. add to this controller:
    def index
    end
  8. create views/home/index.html.erb
    It can be empty file.
  9. Add these lines to app/views/layouts/application.html.erb:
    < div style='color:red'><%= flash[:notice] %>< /div>
    < div style='color:red'><%= flash[:alert] %>< /div>
    < div style='color:green'><%if user_signed_in?%>signed-in<%else%>no_signed<% end%>< /div>
  10. Change initializers/session_store.rb to this line:
    Rails.application.config.session_store :active_record_store
  11. Run:
    rails generate active_record:session_migration
    rake db:migrate
  12. Run application and sign_up using this url:
    /users/sign_up
  13. Sign out (you can do it by clearing cookies)
  14. Try to log_in. Dont use checkbox "remember me".
    This step doesnt work. After form submitted, you will be redirected to / and not signed in.

If you skip step 10 then all will work.
Problem starts when using this: session_store :active_record_store

Then try to apply patch, that I described in the beginning.
Then log_in will work.

new release?

Is there a chance for a new release soon? I mainly want to have a release with 82a7540 to silence the warning/stacktrace with Ruby 2.3.

Replace LoggerSilencer with ActiveSupport LoggerSilence

It is very clear that session_store's approach to log silencing is buggy, comes with interoperability issues, and requires changes to loggers in order to function.

This works as a replacement for lib/active_record/session_store/extension/logger_silencer.rb:

module ActiveRecord
  module SessionStore
    module Extension
      module LoggerSilencer

        # Silences the logger for the duration of the block.
        def silence_logger(temporary_level = Logger::ERROR)
          silence(temporary_level) do
            yield self
          end
        end

      end
    end

    class NilLogger
      def self.silence_logger
        yield
      end
    end
  end
end

LoggerSilencer fails by comparing incorrect log_level

Hello,

my unicorn is sometimes serving a blank page, because the logger silencer of ar-session_store 1.0.0 just fails by comparing incorrect log levels. It seems that sometimes there are wrong data types, such as fixnum being compared with a symbol.
I am using this application.rb for my rails app:

config.logger = Logger.new("#{Rails.root}/log/app.log")
config.log_level = :warn

Sometimes, after several requests this happens to my app:

E, [2016-09-02T09:23:12.990343 #21845] ERROR -- : app error: comparison of Fixnum with :debug failed (ArgumentError)
E, [2016-09-02T09:23:12.990421 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/activerecord-session_store-1.0.0/lib/active_record/session_store/extension/logger_silencer.rb:35:in `<'
E, [2016-09-02T09:23:12.990463 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/activerecord-session_store-1.0.0/lib/active_record/session_store/extension/logger_silencer.rb:35:in `add_with_threadsafety'  E, [2016-09-02T09:23:12.990500 #21845] ERROR -- : /usr/local/rvm/rubies/ruby-2.2.2/lib/ruby/2.2.0/logger.rb:434:in `info'
E, [2016-09-02T09:23:12.990534 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/rack-timeout-0.4.2/lib/rack/timeout/logging-observer.rb:42:in `log_state_change'
E, [2016-09-02T09:23:12.990568 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/rack-timeout-0.4.2/lib/rack/timeout/core.rb:190:in `call'
E, [2016-09-02T09:23:12.990602 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/rack-timeout-0.4.2/lib/rack/timeout/core.rb:190:in `block in notify_state_change_observers'
E, [2016-09-02T09:23:12.990635 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/rack-timeout-0.4.2/lib/rack/timeout/core.rb:190:in `each'
E, [2016-09-02T09:23:12.990668 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/rack-timeout-0.4.2/lib/rack/timeout/core.rb:190:in `notify_state_change_observers'
E, [2016-09-02T09:23:12.990701 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/rack-timeout-0.4.2/lib/rack/timeout/core.rb:167:in `_set_state!'
E, [2016-09-02T09:23:12.990734 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/rack-timeout-0.4.2/lib/rack/timeout/core.rb:106:in `call'
E, [2016-09-02T09:23:12.990791 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/activesupport-4.2.5.2/lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
E, [2016-09-02T09:23:12.990827 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/actionpack-4.2.5.2/lib/action_dispatch/middleware/static.rb:116:in `call'
E, [2016-09-02T09:23:12.990861 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/rack-1.6.4/lib/rack/sendfile.rb:113:in `call'
E, [2016-09-02T09:23:12.990893 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/railties-4.2.5.2/lib/rails/engine.rb:518:in `call'
E, [2016-09-02T09:23:12.990925 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/railties-4.2.5.2/lib/rails/application.rb:165:in `call'
E, [2016-09-02T09:23:12.990958 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/railties-4.2.5.2/lib/rails/railtie.rb:194:in `public_send'
E, [2016-09-02T09:23:12.991028 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/railties-4.2.5.2/lib/rails/railtie.rb:194:in `method_missing'
E, [2016-09-02T09:23:12.991064 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/unicorn-5.1.0/lib/unicorn/http_server.rb:562:in `process_client'
E, [2016-09-02T09:23:12.991096 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/unicorn-5.1.0/lib/unicorn/http_server.rb:658:in `worker_loop'
E, [2016-09-02T09:23:12.991128 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/unicorn-5.1.0/lib/unicorn/http_server.rb:508:in `spawn_missing_workers'
E, [2016-09-02T09:23:12.991160 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/unicorn-5.1.0/lib/unicorn/http_server.rb:132:in `start'
E, [2016-09-02T09:23:12.991192 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/gems/unicorn-5.1.0/bin/unicorn_rails:209:in `<top (required)>'
E, [2016-09-02T09:23:12.991224 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/bin/unicorn_rails:23:in `load'
E, [2016-09-02T09:23:12.991255 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/bin/unicorn_rails:23:in `<main>'
E, [2016-09-02T09:23:12.991286 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `eval'
E, [2016-09-02T09:23:12.991318 #21845] ERROR -- : /usr/local/rvm/gems/ruby-2.2.2/bin/ruby_executable_hooks:15:in `<main>'

I've just dirty fixed that by simply rescue'ing this, cause a simple fix checking the data types didn't cleanly solved that issue.
Looking further to see a solution here.

ERROR: foreign key constraint "fk_sessions_session_id" cannot be implemented

Hi!

I get this error doing rake db:migrate! Any idea?

Rails: 4.1.7
DB adapter: postgresql

Here the session config file:

# config/initializers/session_store.rb

Rails.application.config.session_store :active_record_store

Here the generated migration file:

# db/migrate/***_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

end

Here the migration error log:

ActiveRecord::StatementInvalid: PG::DatatypeMismatch: ERROR:  foreign key constraint "fk_sessions_session_id" cannot be implemented
DETAIL:  Key columns "session_id" and "id" are of incompatible types: character varying and integer.
: CREATE TABLE "sessions" ("id" serial primary key, "session_id" character varying(255) NOT NULL, "data" text, "created_at" timestamp, "updated_at" timestamp, CONSTRAINT fk_sessions_session_id FO
REIGN KEY ("session_id") REFERENCES "sessions" ("id")) 

undefined method `silence_logger' for Logging::Logger

Hi,

I'm using logging 2.0.0 and i have the following error:

silence_logger' for <Logging::Logger:0x3fc0dba74b54 name="ActiveRecord::Base">:Logging::Logger' duration=1.58 view=0.00 db=0.00
[2016-02-28T13:02:49] FATAL Rails : 
NoMethodError (undefined method `silence_logger' for <Logging::Logger:0x3fc0dba74b54 name="ActiveRecord::Base">:Logging::Logger):
  activerecord-session_store (0.1.2) lib/action_dispatch/session/active_record_store.rb:65:in `get_session'
  rack (1.6.4) lib/rack/session/abstract/id.rb:266:in `load_session'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/session/abstract_store.rb:43:in `block in load_session'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/session/abstract_store.rb:51:in `stale_session_check!'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/session/abstract_store.rb:43:in `load_session'
  actionpack (4.2.5.1) lib/action_dispatch/request/session.rb:180:in `load!'
  actionpack (4.2.5.1) lib/action_dispatch/request/session.rb:172:in `load_for_read!'
  actionpack (4.2.5.1) lib/action_dispatch/request/session.rb:89:in `[]'
  warden (1.2.6) lib/warden/session_serializer.rb:30:in `fetch'
  warden (1.2.6) lib/warden/proxy.rb:212:in `user'
  warden (1.2.6) lib/warden/proxy.rb:322:in `_perform_authentication'
  warden (1.2.6) lib/warden/proxy.rb:104:in `authenticate'
  devise (3.5.6) lib/devise/controllers/helpers.rb:124:in `current_user'
  paper_trail (4.1.0) lib/paper_trail/frameworks/rails/controller.rb:35:in `rescue in user_for_paper_trail'
  paper_trail (4.1.0) lib/paper_trail/frameworks/rails/controller.rb:32:in `user_for_paper_trail'
  paper_trail (4.1.0) lib/paper_trail/frameworks/rails/controller.rb:79:in `set_paper_trail_whodunnit'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:432:in `block in make_lambda'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:164:in `call'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:164:in `block in halting'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:504:in `call'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:504:in `block in call'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:504:in `each'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:504:in `call'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:92:in `__run_callbacks__'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:778:in `_run_process_action_callbacks'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.5.1) lib/abstract_controller/callbacks.rb:19:in `process_action'
  actionpack (4.2.5.1) lib/action_controller/metal/rescue.rb:29:in `process_action'
  actionpack (4.2.5.1) lib/action_controller/metal/instrumentation.rb:32:in `block in process_action'
  activesupport (4.2.5.1) lib/active_support/notifications.rb:164:in `block in instrument'
  activesupport (4.2.5.1) lib/active_support/notifications/instrumenter.rb:20:in `instrument'
  activesupport (4.2.5.1) lib/active_support/notifications.rb:164:in `instrument'
  actionpack (4.2.5.1) lib/action_controller/metal/instrumentation.rb:30:in `process_action'
  actionpack (4.2.5.1) lib/action_controller/metal/params_wrapper.rb:250:in `process_action'
  activerecord (4.2.5.1) lib/active_record/railties/controller_runtime.rb:18:in `process_action'
  actionpack (4.2.5.1) lib/abstract_controller/base.rb:137:in `process'
  actionview (4.2.5.1) lib/action_view/rendering.rb:30:in `process'
  actionpack (4.2.5.1) lib/action_controller/metal.rb:196:in `dispatch'
  actionpack (4.2.5.1) lib/action_controller/metal/rack_delegation.rb:13:in `dispatch'
  actionpack (4.2.5.1) lib/action_controller/metal.rb:237:in `block in action'
  actionpack (4.2.5.1) lib/action_dispatch/routing/route_set.rb:74:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/routing/route_set.rb:74:in `dispatch'
  actionpack (4.2.5.1) lib/action_dispatch/routing/route_set.rb:43:in `serve'
  actionpack (4.2.5.1) lib/action_dispatch/journey/router.rb:43:in `block in serve'
  actionpack (4.2.5.1) lib/action_dispatch/journey/router.rb:30:in `each'
  actionpack (4.2.5.1) lib/action_dispatch/journey/router.rb:30:in `serve'
  actionpack (4.2.5.1) lib/action_dispatch/routing/route_set.rb:815:in `call'
  bullet (5.0.0) lib/bullet/rack.rb:12:in `call'
  i18n-js (2.1.2) lib/i18n-js/middleware.rb:11:in `call'
  warden (1.2.6) lib/warden/manager.rb:35:in `block in call'
  warden (1.2.6) lib/warden/manager.rb:34:in `catch'
  warden (1.2.6) lib/warden/manager.rb:34:in `call'
  rack (1.6.4) lib/rack/etag.rb:24:in `call'
  rack (1.6.4) lib/rack/conditionalget.rb:25:in `call'
  rack (1.6.4) lib/rack/head.rb:13:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/params_parser.rb:27:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/flash.rb:260:in `call'
  rack (1.6.4) lib/rack/session/abstract/id.rb:225:in `context'
  rack (1.6.4) lib/rack/session/abstract/id.rb:220:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/cookies.rb:560:in `call'
  activerecord (4.2.5.1) lib/active_record/query_cache.rb:36:in `call'
  activerecord (4.2.5.1) lib/active_record/connection_adapters/abstract/connection_pool.rb:653:in `call'
  activerecord (4.2.5.1) lib/active_record/migration.rb:377:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:88:in `__run_callbacks__'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:778:in `_run_call_callbacks'
  activesupport (4.2.5.1) lib/active_support/callbacks.rb:81:in `run_callbacks'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/reloader.rb:73:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/remote_ip.rb:78:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  lograge (0.3.6) lib/lograge/rails_ext/rack/logger.rb:15:in `call_app'
  railties (4.2.5.1) lib/rails/rack/logger.rb:22:in `call'
  quiet_assets (1.1.0) lib/quiet_assets.rb:27:in `call_with_quiet_assets'
  request_store (1.3.0) lib/request_store/middleware.rb:9:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/request_id.rb:21:in `call'
  rack (1.6.4) lib/rack/methodoverride.rb:22:in `call'
  rack (1.6.4) lib/rack/runtime.rb:18:in `call'
  activesupport (4.2.5.1) lib/active_support/cache/strategy/local_cache_middleware.rb:28:in `call'
  rack (1.6.4) lib/rack/lock.rb:17:in `call'
  actionpack (4.2.5.1) lib/action_dispatch/middleware/static.rb:116:in `call'
  rack (1.6.4) lib/rack/sendfile.rb:113:in `call'
  railties (4.2.5.1) lib/rails/engine.rb:518:in `call'
  railties (4.2.5.1) lib/rails/application.rb:165:in `call'
  rack (1.6.4) lib/rack/content_length.rb:15:in `call'
  thin (1.6.4) lib/thin/connection.rb:86:in `block in pre_process'
  thin (1.6.4) lib/thin/connection.rb:84:in `catch'
  thin (1.6.4) lib/thin/connection.rb:84:in `pre_process'
  thin (1.6.4) lib/thin/connection.rb:53:in `process'
  thin (1.6.4) lib/thin/connection.rb:39:in `receive_data'
  eventmachine (1.0.9.1) lib/eventmachine.rb:193:in `run_machine'
  eventmachine (1.0.9.1) lib/eventmachine.rb:193:in `run'
  thin (1.6.4) lib/thin/backends/base.rb:73:in `start'
  thin (1.6.4) lib/thin/server.rb:162:in `start'
  rack (1.6.4) lib/rack/handler/thin.rb:19:in `run'
  rack (1.6.4) lib/rack/server.rb:286:in `start'
  railties (4.2.5.1) lib/rails/commands/server.rb:80:in `start'
  railties (4.2.5.1) lib/rails/commands/commands_tasks.rb:80:in `block in server'
  railties (4.2.5.1) lib/rails/commands/commands_tasks.rb:75:in `tap'
  railties (4.2.5.1) lib/rails/commands/commands_tasks.rb:75:in `server'
  railties (4.2.5.1) lib/rails/commands/commands_tasks.rb:39:in `run_command!'
  railties (4.2.5.1) lib/rails/commands.rb:17:in `<top (required)>'
  bin/rails:9:in `require'
  bin/rails:9:in `<top (required)>'
  spring (1.6.4) lib/spring/client/rails.rb:28:in `load'
  spring (1.6.4) lib/spring/client/rails.rb:28:in `call'
  spring (1.6.4) lib/spring/client/command.rb:7:in `call'
  spring (1.6.4) lib/spring/client.rb:28:in `run'
  spring (1.6.4) bin/spring:49:in `<top (required)>'
  spring (1.6.4) lib/spring/binstub.rb:11:in `load'
  spring (1.6.4) lib/spring/binstub.rb:11:in `<top (required)>'
  bin/spring:13:in `require'
  bin/spring:13:in `<top (required)>'
  bin/rails:3:in `load'
  bin/rails:3:in `<main>'

It comes from:

Logger.send :include, ActiveRecord::SessionStore::Extension::LoggerSilencer

What it can be?

Flash messages isn't store in session

Rails and Ruby version

Rails 3.2.13  && Ruby 1.9.3

Set database storing In config/initializers/session_store.rb

EXplatform::Application.config.session_store :active_record_store

Set flash messages in controller action

def create
  user = User.create(params[:user])
  flash[:success] = "user is created successfully."
  redirect_to user
end

show flash message in view

<p><%= flash[:success] %></p>

I can't see any messages on browser.
but I change the way of session store cookie, it's OK.
Why?

Rails 4 Custom Session Class

In Rails 4, setting ActiveRecord::SessionStore.session_class = Session in config/session_store.rb returns NoMethodError: undefined method 'session_class=' for ActiveRecord::SessionStore:Module. The same assignment seems to work fine in Rails 3.2.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.