2010-07-09 151 views
7

我在报告模型中定义了一个问题方法。我需要在定义动作展示的同时在报表的控制器中使用Report.problem的值。但我不断收到错误消息“未定义的方法问题”。我如何解决这个问题?任何援助都会很棒。在模型中定义可以在控制器中访问的方法

我有一个包含所有问题列表的报告模型和问题模型。

在报表模型

def problems1 
Problem.find(:all, :conditions =>) 
end 

在报告控制器我需要这样的东西

def show 
    @report = Report.problems1 
end 
+0

你能添加什么是失败的一些代码?您正在尝试调用的方法的模型代码和生成错误的控制器代码。 – 2010-07-09 04:41:24

回答

26

你必须分配self.method_name作为一个类的方法来使用

遵循型号方法如下规则

类方法

def self.problem 

end 

在控制器

Report.problem 

实例方法

def problem 

end 
在控制器

report = Report.new 
report.problem 
+0

谢谢,我会牢记在心 – Prateek 2010-07-09 05:12:47

0

如果你定义的方法类方法

class Report < ActiveRecord :: Base 
def Report.problem 
    puts 1 
end 
end 

Report.problem 
>1 

但是,如果你定义的方法对象

class Report < ActiveRecord :: Base 
def problem 
    puts 1 
end 
end 

此方法调用

report = Report.new 
report.problem 
>1 
+0

谢谢,有帮助。我可以在控制器中使用它? – Prateek 2010-07-09 05:07:55

相关问题