【发布时间】:2015-11-30 23:56:37
【问题描述】:
我的视图助手目录中有一个方法,我试图在模型中使用它,但我不断收到未定义的方法错误。我无法弄清楚我做错了什么。这是我的模块。
module StbHelper
def gen_csv(stbs)
CSV.generate do |csv|
csv << [
'param1',
'param2'
]
stbs.each do |stb|
health_check = stb.stb_health_checks.last
csv << [
'value1',
'value2'
]
end
end
end
这是我要在其中使用方法的类。
require 'stb_helper'
class Stb < ActiveRecord::Base
def self.get_notes_data
.
.
.
end
def self.update
.
.
.
end
def self.report(options={})
csv_file = nil
if options == {}
########################################
# This is the line that throws the error
csv_file = StbHelper.gen_csv(Stb.all)
#######################################
else
stbs = []
customers = List.where(id: options[:list])[0].customers
customers.each do |customer|
customer.accounts.each do |account|
stbs += account.stbs
end
end
csv_file = StbHelper.gen_csv(stbs)
end
end
end
【问题讨论】:
-
正如您在问题中已经说过的,助手是为了查看。为了在你的模型中使用它see this question,or this tutorial
-
简答:在模型中使用视图助手。视图助手用于视图。看起来您只需要一个简单的实用程序库/类/模块。
-
这些 cmets 让我朝着正确的方向前进。我决定将该方法移至模型并使其成为类级别的方法。现在一切正常
标签: ruby-on-rails ruby activerecord