This blog will undergo a slow migration to the following address: http://www.debugarea.com/
Advertisements
This blog will undergo a slow migration to the following address: http://www.debugarea.com/
Here’s what you need to do to take advantage of AR’s SessionStore. The following was tested with Rails 3.2.5.
YourApp.config.session_store :active_record_store
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:
ActiveRecord::SessionStore.session_class = Session
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
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
t = DateTime.now
t.change(:offset => "+0800")
will change the timezone to a +0800 one.
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
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"
ActiveRecord::Migration.drop_table(:users)