Ruby on Rails活动记录会话

时间:2014-04-04 08:57:09

标签: ruby-on-rails ruby session activerecord

我正在开发一个RoR应用程序。更新到Ruby 2.x和Rails 4.x后,我得到一个CookieOverflowError。为了解决这个问题,我尝试使用Active Record Session来存储会话。我用以下命令设置它:

rails generate active_record:session_migration production
RAILS_ENV=production rake db:migrate

并在config / initializers / session_store.rb中更改了我的配置     AppName :: Application.config.session_store:active_record_store

这出现在log / production.log文件中:

ArgumentError (wrong number of arguments (1 for 2)):
  vendor/bundle/ruby/2.1.0/gems/arel-5.0.0/lib/arel/crud.rb:5:in `compile_update'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/persistence.rb:500:in `update_record'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/locking/optimistic.rb:70:in `update_record'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/attribute_methods/dirty.rb:74:in `update_record'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/callbacks.rb:310:in `block in update_record'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.4/lib/active_support/callbacks.rb:373:in `_run__4115952957871857265__update__callbacks'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.4/lib/active_support/callbacks.rb:80:in `run_callbacks'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/callbacks.rb:310:in `update_record'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/timestamp.rb:70:in `update_record'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/persistence.rb:478:in `create_or_update'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/callbacks.rb:302:in `block in create_or_update'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.4/lib/active_support/callbacks.rb:393:in `_run__4115952957871857265__save__callbacks'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.4/lib/active_support/callbacks.rb:80:in `run_callbacks'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/callbacks.rb:302:in `create_or_update'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/persistence.rb:103:in `save'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/validations.rb:51:in `save'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/attribute_methods/dirty.rb:32:in `save'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/transactions.rb:270:in `block (2 levels) in save'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/transactions.rb:330:in `block in with_transaction_returning_status'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `block in transaction'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:221:in `within_new_transaction'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/database_statements.rb:213:in `transaction'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/transactions.rb:209:in `transaction'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/transactions.rb:327:in `with_transaction_returning_status'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/transactions.rb:270:in `block in save'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/transactions.rb:285:in `rollback_active_record_state!'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/transactions.rb:269:in `save'
  vendor/bundle/ruby/2.1.0/bundler/gems/activerecord-session_store-55dc59aff9a1/lib/action_dispatch/session/active_record_store.rb:80:in `block in set_session'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.4/lib/active_support/core_ext/kernel/reporting.rb:110:in `block (2 levels) in quietly'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.4/lib/active_support/core_ext/kernel/reporting.rb:50:in `silence_stream'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.4/lib/active_support/core_ext/kernel/reporting.rb:109:in `block in quietly'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.4/lib/active_support/core_ext/kernel/reporting.rb:50:in `silence_stream'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.4/lib/active_support/core_ext/kernel/reporting.rb:108:in `quietly'
  vendor/bundle/ruby/2.1.0/bundler/gems/activerecord-session_store-55dc59aff9a1/lib/action_dispatch/session/active_record_store.rb:77:in `set_session'
  vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:342:in `commit_session'
  vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:226:in `context'
  vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/session/abstract/id.rb:220:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.4/lib/action_dispatch/middleware/cookies.rb:486:in `call'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/query_cache.rb:36:in `call'
  vendor/bundle/ruby/2.1.0/gems/activerecord-4.0.4/lib/active_record/connection_adapters/abstract/connection_pool.rb:626:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.4/lib/action_dispatch/middleware/callbacks.rb:29:in `block in call'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.4/lib/active_support/callbacks.rb:373:in `_run__2968889427859779895__call__callbacks'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.4/lib/active_support/callbacks.rb:80:in `run_callbacks'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.4/lib/action_dispatch/middleware/callbacks.rb:27:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.4/lib/action_dispatch/middleware/remote_ip.rb:76:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.4/lib/action_dispatch/middleware/debug_exceptions.rb:17:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.4/lib/action_dispatch/middleware/show_exceptions.rb:30:in `call'
  vendor/bundle/ruby/2.1.0/gems/railties-4.0.4/lib/rails/rack/logger.rb:38:in `call_app'
  vendor/bundle/ruby/2.1.0/gems/railties-4.0.4/lib/rails/rack/logger.rb:20:in `block in call'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.4/lib/active_support/tagged_logging.rb:68:in `block in tagged'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.4/lib/active_support/tagged_logging.rb:26:in `tagged'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.4/lib/active_support/tagged_logging.rb:68:in `tagged'
  vendor/bundle/ruby/2.1.0/gems/railties-4.0.4/lib/rails/rack/logger.rb:20:in `call'
  vendor/bundle/ruby/2.1.0/gems/actionpack-4.0.4/lib/action_dispatch/middleware/request_id.rb:21:in `call'
  vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/methodoverride.rb:21:in `call'
  vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/runtime.rb:17:in `call'
  vendor/bundle/ruby/2.1.0/gems/activesupport-4.0.4/lib/active_support/cache/strategy/local_cache.rb:83:in `call'
  vendor/bundle/ruby/2.1.0/gems/rack-1.5.2/lib/rack/sendfile.rb:112:in `call'
  vendor/bundle/ruby/2.1.0/gems/railties-4.0.4/lib/rails/engine.rb:511:in `call'
  vendor/bundle/ruby/2.1.0/gems/railties-4.0.4/lib/rails/application.rb:97:in `call'
  vendor/bundle/ruby/2.1.0/gems/railties-4.0.4/lib/rails/railtie/configurable.rb:30:in `method_missing'
  /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.40/lib/phusion_passenger/rack/thread_handler_extension.rb:74:in `process_request'
  /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.40/lib/phusion_passenger/request_handler/thread_handler.rb:141:in `accept_and_process_next_request'
  /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.40/lib/phusion_passenger/request_handler/thread_handler.rb:109:in `main_loop'
  /usr/local/rvm/gems/ruby-2.1.1/gems/passenger-4.0.40/lib/phusion_passenger/request_handler.rb:448:in `block (3 levels) in start_threads'

我创建了一个单独的日志文件,用于打印记录的内容,调用方法保存。保存失败的方法(active_record_store.rb)。

D, [2014-04-04T11:18:09.058984 #31574] DEBUG -- : #<ActiveRecord::SessionStore::Session id: 1, session_id: "11f19fdf6d404a4667f11458bedf157f", data: "BAh7BkkiEGluaXRpYWxfdXJpBjoGRVRJIi1odHRwczovL2ljb3N...", created_at: "2014-04-04 08:47:18", updated_at: "2014-04-04 08:47:18">

从rails控制台:

2.1.1 :001 > ActiveRecord::Base.connection.tables
 => ["schema_migrations", "items", "roles", "rights", "roles_users", "rights_roles", "users", "queue_messages", "mammie_tokens", "sessions"]
2.1.1 :002 > ActiveRecord::Base.connection.columns('sessions')
 => [#<ActiveRecord::ConnectionAdapters::SQLite3Column:0x00000002a80078 @name="id", @sql_type="INTEGER", @null=false, @limit=nil, @precision=nil, @scale=nil, @type=:integer, @default=nil, @default_function=nil, @primary=nil, @coder=nil>, #<ActiveRecord::ConnectionAdapters::SQLite3Column:0x00000002a62a00 @name="session_id", @sql_type="varchar(255)", @null=false, @limit=255, @precision=nil, @scale=nil, @type=:string, @default=nil, @default_function=nil, @primary=nil, @coder=nil>, #<ActiveRecord::ConnectionAdapters::SQLite3Column:0x00000002a60c00 @name="data", @sql_type="text", @null=true, @limit=nil, @precision=nil, @scale=nil, @type=:text, @default=nil, @default_function=nil, @primary=nil, @coder=nil>, #<ActiveRecord::ConnectionAdapters::SQLite3Column:0x00000002a60840 @name="created_at", @sql_type="datetime", @null=true, @limit=nil, @precision=nil, @scale=nil, @type=:datetime, @default=nil, @default_function=nil, @primary=nil, @coder=nil>, #<ActiveRecord::ConnectionAdapters::SQLite3Column:0x00000002a602c8 @name="updated_at", @sql_type="datetime", @null=true, @limit=nil, @precision=nil, @scale=nil, @type=:datetime, @default=nil, @default_function=nil, @primary=nil, @coder=nil>]

Active Record自己的日志文件:

# Logfile created on 2014-04-04 13:13:35 +0200 by logger.rb/44203
D, [2014-04-04T13:13:37.163858 #11512] DEBUG -- :   ActiveRecord::SessionStore::Session Load (0.3ms)  SELECT "sessions".* FROM "sessions" WHERE "sessions"."session_id" = '871b57e38039360f4aeaa61857e90605' ORDER BY "sessions"."id" ASC LIMIT 1
D, [2014-04-04T13:13:37.176779 #11512] DEBUG -- :   User Load (0.3ms)  SELECT "users".* FROM "users" WHERE (UPPER(username) = 'ADMINISTRATOR' and hashed_pasword = 'HASH') ORDER BY "users"."id" ASC LIMIT 1
D, [2014-04-04T13:13:37.216335 #11512] DEBUG -- :    (0.1ms)  begin transaction
D, [2014-04-04T13:13:37.241415 #11512] DEBUG -- :   SQL (20.6ms)  INSERT INTO "mammie_tokens" ("created_at", "token", "updated_at") VALUES (?, ?, ?)  [["created_at", Fri, 04 Apr 2014 11:13:37 UTC +00:00], ["token", "token_17a75000-9518-0131-0160-2565bbb3839e"], ["updated_at", Fri, 04 Apr 2014 11:13:37 UTC +00:00]]
D, [2014-04-04T13:13:37.252799 #11512] DEBUG -- :    (10.8ms)  commit transaction
D, [2014-04-04T13:13:37.264598 #11512] DEBUG -- :    (0.2ms)  begin transaction
D, [2014-04-04T13:13:37.272679 #11512] DEBUG -- :    (0.2ms)  rollback transaction

然而,我一遍又一遍地得到同样的错误,因为当(我猜)某些事情被保存时出现了问题。

有什么建议吗?

2 个答案:

答案 0 :(得分:0)

您必须检查,您没有在会话中存储任何大数据文件以及任何大小大于“data”db列大小的数据。

答案 1 :(得分:0)

我认为问题是由于GEM不兼容。我最近更新了active_records和arel GEMS,这些已知容易出现兼容性问题。在这一行:

  vendor/bundle/ruby/2.1.0/gems/arel-5.0.0/lib/arel/crud.rb:5:in `compile_update'

函数compile_update需要两个参数,但显然只得到一个,因为使用了不兼容的active_records和adel版本。