开发者

In ruby, how to create a method that has a options hash, and I can pass symbols to it?

In my Article model (rails 3), I want to create methods that takes a parameter.

So开发者_StackOverflow社区 I can do things like:

Article.get_by_id(:cache => true, :cache_expire => some_date)

I want to re-use this parameter, how can I do this?

The parameter is a hash of options, and I want to use it in many of my controllers methods.


you can add following method to Article model

self.get_by_id(options = {}) 
cache = options['cache']
end

Now the parameter sent to the above method can be accessed using option hash. i.e, options['cache'] and options['cache_expired'].


Do you mean like this?

custom_options = { :cache => true, :cache_expire => some_date }
Article.get_by_id(custom_options)

I'm sorry if I am not getting you but your question was very brief.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜