Archive for July, 2012

18
Jul
12

blog movement

This blog will undergo a slow migration to the following address: http://www.debugarea.com/

05
Jul
12

using the activerecord session_store for session tracking


Here’s what you need to do to take advantage of AR’s SessionStore. The following was tested with Rails 3.2.5.

    1. Run rake db:sessions:create
    2. Run rake db:migrate
    3. Modify config/initializers/session_store.rb to this:
       YourApp.config.session_store :active_record_store

 

  • Add the following in application.rb:
     config.action_dispatch.session_store = :active_record_store  

 

This will setup the default session tracking mechanism. I wanted to have a custom Session class, which held the user_id ( if he’s logged in ). Here’s the additional things I’ve done:

    1. I added in config/initializers/session_store.rb the following line:
       ActiveRecord::SessionStore.session_class = Session

 

  • I added the following code in 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 
          user_id = warden_data[1][0] 
          self.user = User.find(user_id) 
        end 
      end 
    end 

 

 

04
Jul
12

creating a postgresql schema from inside a rails migration

If you ever need to create a schema from inside a migration, write this:


def up
  execute "CREATE SCHEMA some_schema"
end

def down
  execute "DROP SCHEMA some_schema"
end



Blog Stats

  • 218,505 hits