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
26
Jun
12

change timezone without changing time


t = DateTime.now
t.change(:offset => "+0800")

will change the timezone to a +0800 one.

25
Jun
12

rendering a jbuilder template to a string

Here’s how:


#controllers/users_controller.rb
def index
  @users = User.all
  @users_json = render_to_string( template: 'users.json.jbuilder', locals: { users: @users})
end

#views/users/users.json.jbuilder
json.array!(users) do |json, user|
  json.(user, :id, :name)
end

Thanks to Aaron Renoir

22
Jun
12

Dalli unmarshal error

I’ve kept receiving errors like: DalliError: unable to unmarshal value.
The solution was to add all the unmarshable classes in the relevant controllers, like this:


require_dependency "some_model_that_cannot_be_unmarshalled"
20
Jun
12

rails delete table from console


ActiveRecord::Migration.drop_table(:users)
09
Jun
12

emacs 24 ubuntu


sudo add-apt-repository ppa:cassou/emacs

sudo apt-get remove `apt-cache search emacs | awk '{print $1}'` --purge

sudo apt-get -f install # don't list packages here

sudo apt-get remove emacs-snapshot --purge

sudo apt-get autoclean

sudo apt-get autoremove

sudo apt-get update

sudo apt-get install emacs-snapshot

Information found: here




Blog Stats

  • 281,573 hits