Alexey Poimtsev
Alexey Poimtsev

Reputation: 2847

rails3 session store

could you tell me plz - how to use in rails3 application external Active Record session store?

In rails2 its simply

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

but wat about rails3?

Upvotes: 0

Views: 6337

Answers (3)

Ben
Ben

Reputation: 51

Look in config/initializers/session_store.rb

comment out the line about using :cookie_store

uncomment the lines at the bottom about using :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

Note: "MyApp" will be the name of your application.

Upvotes: 5

Dan McNevin
Dan McNevin

Reputation: 22336

Looking at the source for activerecord-3.0.0.rc/lib/active_record/session_store.rb I see this:

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

So, you should be able to do something like: ActiveRecord::SessionStore::Session.connection = establish_connection("sessions_#{RAILS_ENV}") but I haven't tested that.

You can also make your own session class that you have more control over how it connects to the database, from the same file:

 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

Upvotes: 1

AboutRuby
AboutRuby

Reputation: 8116

You'll have to use this.

Rails.application.config.session_store :active_record_store

I'm not sure how to set the table name though.

Upvotes: 0

Related Questions