Rails 中常用方法的好地方在哪里?
我有一个方法,我已经开始在多个模型中使用该方法进行网页抓取,最好将其保存在哪里?我应该把它放在application_controller、application_helper中吗?我不知道应该把它放在哪里供多个模型使用?
def self.retryable(options = {}, &block)
opts = { :tries => 1, :on => Exception }.merge(options)
retry_exception, retries = opts[:on], opts[:tries]
begin
return yield
rescue retry_exception
retry if (retries -= 1) > 0
end
yield
end
I have a method that I've started to use in multiple models for Webscraping, where is the best place to keep it? Should I put it in the application_controller, application _helper? I'm not sure where a good place is to put it for multiple models to use it?
def self.retryable(options = {}, &block)
opts = { :tries => 1, :on => Exception }.merge(options)
retry_exception, retries = opts[:on], opts[:tries]
begin
return yield
rescue retry_exception
retry if (retries -= 1) > 0
end
yield
end
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以创建一个模块。来自 Altered Beast 项目的示例:(我经常在其他项目中查看他们如何解决具体问题)
并且在模型中:
You could create a module. An example from the Altered Beast project: (I often look in other projects how they solve specific problems)
And in the models:
将 retryable.rb 放入 lib/
使用它:
或包含命名空间:
Put retryable.rb in lib/
Use it:
or including namespace: