views:

41

answers:

2

Hi, 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?

A: 

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.

AboutRuby
+1  A: 

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
Dan McNevin
correct code isActiveRecord::SessionStore::Session.establish_connection("sessions_#{Rails.env}")thanks a lot!!!
Alexey Poimtsev