|
Tip: Rails Options HelpersWritten By: Anthony Crumley January 9th, 2008 |
Ok, so these are not really helpers but they are helpful. Rails often uses a hash as the last method parameter to pass options. When the method has a variable number of parameters there is a need to pull the options hash out of the arguments array. To do this they added an extract_options! method to the Array. This method removes and returns the last argument in the array if it is a Hash.
def some_method(*args) options = args.extract_options! .. end
Another common options problem is setting default values. Rails added reverse_merge to the Hash object to help out here.
def some_method(*args)
options = args.extract_options!
options.reverse_merge!({:limit => 10})
..
end
November 12th, 2008 at 05:24 PM osw0emu0dr8bp3yu
January 28th, 2009 at 09:13 PM Hi, Thanks, just the info I needed. I didn't want to look for the details or look the Rails code at 4 AM :) Cheers, Rafael.