0

I have a controller class

class MyController < ApplicationController

    def firstMethod
       @obj = #get friends list from FB
    end

    def secondMethod
     puts @obj
    end
end

So in /my/firstMethod , i have

  <%= link_to "Likes","/my/secondMethod" %>

The method is called and page is rendered but "puts @obj" display blank.

I know one way is to use before_filter but since its a expensive operation i want to get list of friends only once.

Any pointers ?

Harshit

3
  • You could use a class variable (@@obj), but unless you have verified by profiling that this is a performance bottleneck, you should stick with a before_filter. Also, Rails has caching, which allows you to cache a lot more than your friends list. Also note that in development mode, your controllers are reloaded at every request, so you can only use @@obj as a cache Commented Dec 20, 2011 at 20:30
  • @Niklas Are you sure that a class variable only lives during the time of one request? Commented Dec 20, 2011 at 20:44
  • @NicolasGUILLAUME: yes, I just tested it. Only in development mode, of course. Commented Dec 20, 2011 at 23:45

1 Answer 1

2

I would use a before_filter and store the result in cache.

You can also use the session to store the friend list but I would avoid that except if the stored list has a controlled length.

Sign up to request clarification or add additional context in comments.

1 Comment

storing in cache is what i would prefer than session size size is pretty huge.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.