Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 5940313
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T15:57:03+00:00 2026-05-22T15:57:03+00:00

As part of an optimization, we don’t store our entire User in the session,

  • 0

As part of an optimization, we don’t store our entire User in the session, instead we wrap the User inside a facade which contains only the basic attributes, such as id, username and admin?. Anything else is lazy-loaded (using method_missing). When this object is written to the session, it unloads the user prior to marshalling.

Basically:

class UserProxy
  attr_reader :id
  attr_reader :username

  def initialize(user)
    @user = user
    reload_attributes!
  end

  def admin?
    @admin
  end

  def self.model_name
    User.model_name
  end

  def method_missing(meth, *args, &block)
    @user ||= User.get!(id)
    @user.send(meth, *args, &block)
  ensure
    reload_attributes! # seems as good a place as any to keep the data fresh
  end

  def unset_user!
    @user = nil
    self
  end

  def ==(*args, &block)
    method_missing('==', *args, &block)
  end

  def !=(*args, &block)
    method_missing('!=', *args, &block)
  end

  def self._load(data) # custom unmarshalling logic
    UserProxy.new(ActiveSupport::JSON.decode(data)).unset_user!
  end

  def _dump(depth) # custom marshalling logic
    reload_attributes!
    ActiveSupport::JSON.encode({"id" => id, "username" => username, "admin" => admin})
  end

  private

    def reload_attributes!
      return if @user.nil?

      @id = @user["id"]
      @username = @user["username"]
      @admin = @user["admin"]
    end
end

This works great everywhere (it behaves just like the DataMapper user it wraps and we don’t have to hit the database to get the user on most page requests, since the important bits (key and username) are already loaded.

What it doesn’t play nicely with, is the path helpers Rails creates.

<%= url_for(@user) # should be /users/1234 %>

Initially it would error badly because it couldn’t figure out what the model_name was, so I fixed that by adding the class method model_name to my UserProxy class and just returning User.model_name and it now finds the correct routes, but in place of the user ID, it puts:

<a href="/users/%23%3CUserProxy:0xa4449cc%3E">Chris Corbyn</a>

I’m not sure where to look to fix this one. I’ve put debug calls in method_missing to see what is being invoked, but it’s not even receiving a call to to_param, like I’d expect it to.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-22T15:57:04+00:00Added an answer on May 22, 2026 at 3:57 pm

    I can’t believe I didn’t try this. I just added to_param to the proxy and delegated the call to the user object and it works. I didn’t realize all objects in Rails had a to_param method, model or not. I should probably do the same for to_key then!

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Part of a complex query that our app is running contains the lines: ...(inner
Part of our website is protected with .htaccess style password protection. When you try
After finishing coding for my app, I started to get into the optimization part.
Is fzero in the standard MATLAB package or is it part of the optimization
I know rule #1 of optimization is: don't do it! But I figured this
Part of the GUI I'm building using tkinter has a pop-up window that says
Part of my web application is a background script that polls from a beanstalkd
Part of my app caches web pages for offline viewing. To do that, I
Part of my table design is to include a IsDeleted BIT column that is
Part of my code I get the OuterHTML propery <LI onclick=TabClicked(this, 'SearchName', 'TabGroup1');>Name so

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.