I want to put logic in the model rather than controller.
class UsersController < ApplicationController
def somemethod
d = User.methodinmodel
end
end
class User < ActiveRecord::Base
def methodinmodel
"retuns foo"
end
end
I get an error that there is no methodinmodel for the User model.
Why?
If you want to be able to call
methodinmodelon theUserclass in general rather than a specific user, you need to make it a class method usingself:Your current method definition would only work if you called it on a user:
Using the new implementation using
selfwould allow you to call it like: