views:

22

answers:

2

I have defined a problems method in my Report model. I need to use the value of Report.problem in the report's controller while defining the action show. But i keep getting the error message 'undefined method problem'. How do i solve this? Any assistance would be greatful.

I have a report model and a problem model that contains a list of all problems.

In report model

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

In the reports controller i need something like

def show
  @report = Report.problems1
end
+1  A: 

If you define method as class method

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

Report.problem
>1

But if you define method as object

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

This method call

report = Report.new
report.problem
>1
Exorcist
Thanks that helps. I can use this in the controller right?
Prateek
+1  A: 

you have to assign self.method_name to use as a class method

Follow following rule for Model methods

Class Method

def self.problem

end

in controller

Report.problem

Instance method

def problem

end

in controller

report =  Report.new
report.problem
Salil
Thanks, i'll keep it in in mind
Prateek