Slicekick
Slicekick

Reputation: 2159

Ruby saving "Model.all" into application-wide variable

I have a model called Business, and I want to save Business.all into a variable I can access from another part of my Rails application. What is the best way to do this? I am fairly new to Ruby/Ruby on Rails and I know of class and instance variables but I am a bit cloudy on this.

Thanks!

Upvotes: 0

Views: 71

Answers (2)

GoGoGarrett
GoGoGarrett

Reputation: 623

If you want this to be accessible throughout the entire application, you could put it into the application controller found in app/controllers/application_controller.rb.

Example:

class ApplicationController < ActionController::Base
  protect_from_forgery
  before_filter :find_all_businesses

  def find_all_businesses
    @businesses = Business.all 
  end
end

Hope this helps.

Upvotes: 1

Tom Livesey
Tom Livesey

Reputation: 411

You probably want to look at Rails caching. There is a good screencast here:

http://railscasts.com/episodes/115-caching-in-rails-2-1

It also applies to rails 3. This will allow you to expire the cache if a business is added for example.

Upvotes: 0

Related Questions