Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

define method in model that can be accessed in controller

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
like image 682
Prateek Avatar asked Jul 09 '10 04:07

Prateek


People also ask

What is controller in Rails?

The Rails controller is the logical center of your application. It coordinates the interaction between the user, the views, and the model. The controller is also a home to a number of important ancillary services. It is responsible for routing external requests to internal actions.


1 Answers

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
like image 73
Salil Avatar answered Sep 21 '22 19:09

Salil