You are here: Forums Ask a Rails expert Static Objects in Rails...
Posted in Forums : Ask a Rails expert
Authority 0
Posting Rating 0
Sign in to rate this post
|
Hi, I want to create some static objects in rails that get created on startup. What is the best way to do this and where is best to put them for visibility from views (and possible controllers)? Tks, |
Authority 12
Posting Rating 89
Sign in to rate this post
|
You can define them as constants and place them in your environment.rb file Example SITE_TITLE = ‘Welcome to my website’ |
Authority 37
Posting Rating 95
Sign in to rate this post
|
If you need them to be visible in views and controllers only (i.e. not in models), you could always define instance variables in your ApplicationController and load them with a before_filter. Imagine a shop site where you always need to display some kind of product list:
class ApplicationController < ActionController::Base
before_filter :load_products
# ... other code
private
def load_products
@products = Product.find(:all)
end
end
That’s what probably most developers would consider the Rails way … HTH |
Authority 0
Posting Rating 0
Sign in to rate this post
|
Hi Clemens, Won’t load_products get called every time a controller method gets called? Sound like alot of overhead. Imagine the case where what you are loading are almost never going to change. |
|
Authority 62
Posting Rating 0
Sign in to rate this post
|
Hi, I like to create a module in my lib folder with all these values. I also prefer to have them defined as class/module variables rather than as constants. That way, if I need to override a value (for example, in a initializer file) I will not get a warning. You could put a file named my_config.rb in your lib folder with the following content module MyConfig Then from any place in your code (model, view, controller, helper, lib method, whatever) you can do something like If you prefer to use constants, you can do something like module MyConfig and then use it like MyConfig::MY_CONSTANT Regards, |
|
Authority 37
Posting Rating 92
Sign in to rate this post
|
You may want to check out the plugin code here:
|
Authority 37
Posting Rating 95
Sign in to rate this post
|
Michael: The plugin that Cuong mentioned is quite nifty. Other than that, Ryan Bates shows a great example in his Railscasts’ episode #85 (http://railscasts.com/episodes/85) where he uses YAML to store the config. You might want to take a look at this option. |
Authority 12
Posting Rating 98
Sign in to rate this post
|
There is a really useful bit of code in section 61 of the Advanced Rails Recipes book. It’s called ConstantCache. I use a modified version of the code from the book in my app, but you can get it as a plugin now: http://github.com/vigetlabs/constant_cache In my app I have a few different Role objects, with title “Admin”, “User” etc. With this plugin I can modify my model by adding a “caches_constants” line like this:
now when my rails app loads up, all of the different roles are loaded, and constants are added to the Role class. So in my authentication code I can refer to Role::ADMIN or Role::USER. The instances of Role which are associated with each constant are only loaded once, when the application starts up. |
Ask a Rails expert : Tracking down an issue
Ask a Rails expert : Thread Vs Transaction
Ask a Rails expert : implementing whitelist plugin
Ask a Rails expert : Validation helper
Ask a Rails expert : FILE EDIT/DELETE
Ask a Rails expert : decrypting the password
Ask a Rails expert : Static Objects in Rails
Ask a Rails expert : Newbie - Rake Cant Execute Test
Ask a Rails expert : Call Web service
Ask a Rails expert : RMagick issues