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 3985960
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T05:57:15+00:00 2026-05-20T05:57:15+00:00

I am a rails newbie trying to implement caching for my app. I installed

  • 0

I am a rails newbie trying to implement caching for my app.
I installed memcached and configured it in my development.rb as follows:

config.action_controller.perform_caching             = true
config.cache_store = :mem_cache_store

I have a controller ProductsController that shows user specific products when they are logged in.

class ProductsController < ApplicationController
  caches_action :index, :layout => false
  before_filter :require_user

  def index
    @user.products              
  end
end

The route for index action is: /products

The problem is that when I login as

1) User A for the first time, rails hits my controller and caches the products action.

2) I logout and login as User B, it still logs me in as User A and shows products for User A and not User B.
It doesn’t even hit my controller.

The key probably is the route, in my memcached console I see that it’s fetching based on the same key.

20 get views/localhost:3000/products
20 sending key views/localhost:3000/products

Is action caching not what I should use? How would I cache and display user specific products?

Thanks for your help.

  • 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-20T05:57:16+00:00Added an answer on May 20, 2026 at 5:57 am

    The first problem is that your before_filter for require_user is after the action caching, so that won’t run. To fix that, use this controller code:

    class ProductsController < ApplicationController
      before_filter :require_user
      caches_action :index, :layout => false
    
      def index
        @products = @user.products              
      end
    end
    

    Second, for action caching you are doing the exact same thing as page caching, but after filters are run, so your @user.products code won’t run. There are a couple of ways to fix this.

    First, if you want you can cache the action based on the parameters that are passed to the page. For example, if you pass a user_id parameter, you could cache based on that parameter like this:

    caches_action :index, :layout => false, :cache_path => Proc.new { |c| c.params[:user_id] }
    

    Second, if you want to simply cache the query and not the entire page, you should remove action caching entirely and only cache the query, like this:

    def index
      @products = Rails.cache.fetch("products/#{@user.id}"){ @user.products }
    end
    

    This should help you get on your way to having a separate cache for each user.

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

Sidebar

Related Questions

Newbie question on Rails 3 development. I am creating my first app and planning
Complete rails newbie trying to get started. I have two classes, Ingredient, and Unit.
I'm Ruby/Rails newbie and trying to get the laika tool ( http://github.com/CCHIT/laika/tree/master ) running
Rails newbie here, trying to get a new controller working. When I try to
I'm a rails newbie, I've been trying to figure out what is going on
I'm somewhat of a Rails newbie. I'm writing a couchrest-rails app, so am not
I'm a newbie Rails developer who is getting the following error when trying to
I'm a newbie in Rails and I'm trying to deploy my first project. I'm
Here is one more newbie question: require 'tasks/rails' I saw this line in Rakefile
I'm a Git newbie. I recently moved a Rails project from Subversion to Git.

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.