rails3会话商店

时间:2010-08-26 22:01:00

标签: ruby-on-rails ruby activerecord ruby-on-rails-3

你能告诉我plz - 如何在rails3应用程序外部Active Record会话存储中使用?

在rails2中,它只是

ActiveRecord::SessionStore::Session.establish_connection("sessions_#{RAILS_ENV}")

但是关于rails3?

3 个答案:

答案 0 :(得分:5)

查看config/initializers/session_store.rb

注释掉有关使用:cookie_store

的内容

使用:active_record_store

取消注释底部的行
# Use the database for sessions instead of the cookie-based default,
# which shouldn't be used to store highly confidential information
# (create the session table with "rails generate session_migration")
MyApp::Application.config.session_store :active_record_store

注意:“MyApp”将是您的应用程序的名称。

答案 1 :(得分:1)

查看activerecord-3.0.0.rc/lib/active_record/session_store.rb的来源,我看到了这一点:

165     # The database connection, table name, and session id and data columns
166     # are configurable class attributes.  Marshaling and unmarshaling
167     # are implemented as class methods that you may override.

183       # :singleton-method:
184       # Use the ActiveRecord::Base.connection by default.
185       cattr_accessor :connection

208         def connection
209           @@connection ||= ActiveRecord::Base.connection
210         end

所以,您应该可以执行以下操作:ActiveRecord::SessionStore::Session.connection = establish_connection("sessions_#{RAILS_ENV}")但我没有测试过。

您还可以从同一个文件创建自己的会话类,以便更好地控制它与数据库的连接方式:

 34   # You may provide your own session class implementation, whether a
 35   # feature-packed Active Record or a bare-metal high-performance SQL
 36   # store, by setting
 37   #
 38   #   ActiveRecord::SessionStore.session_class = MySessionClass
 39   #
 40   # You must implement these methods:
 41   #
 42   #   self.find_by_session_id(session_id)
 43   #   initialize(hash_of_session_id_and_data)
 44   #   attr_reader :session_id
 45   #   attr_accessor :data
 46   #   save
 47   #   destroy

答案 2 :(得分:0)

你必须使用它。

Rails.application.config.session_store :active_record_store

我不知道如何设置表名。