问题
If I call a method on a rails active model method like so:
class Foo < ActiveRecord::Base
end
Foo.first
I'll get back the first active record. I don't have to instantiate the class.
But if I create my own class and call a method, I get an exception:
class Person < ActiveRecord::Base
def greeting
'hello'
end
end
Person.greeting
#EXCEPTION: undefined method `greeting' for Person:Class
How can I make that problem go away?
回答1:
There are several kinds of methods. The two most important ones are: instance methods and class instance methods.
Foo.first
is a class instance method. It works on a class instance (Foo
, in this case). If it stores some data inside the class, that data is shared globally across your program (because there's only one class with name Foo (or ::Foo
, to be exact)).
But your greeting
method is an instance method, it requires object instance. If your greeting method will use Person's name, for example, it has to be instance method, so that it will be able to use instance data (the name). If it doesn't use any instance-specific state and you really meant it to be a class instance method, then use the self
"prefix".
class Person < ActiveRecord::Base
def self.greeting
'hello'
end
end
回答2:
Try class methods:
class Person < ActiveRecord::Base
def self.greeting
'hello'
end
end
Or another syntax:
class Person < ActiveRecord::Base
class << self
def greeting
'hello'
end
end
end
回答3:
class Person < ActiveRecord::Base
def Person.greeting
'hello'
end
end
Will work too. I like it because it is very clear what it does; it will result in an error when you decide to rename the Person class however.
回答4:
class Person < ActiveRecord::Base
def self.greeting
'hello'
end
end
回答5:
To do a static method try this:
class MyModel def self.do_something puts "this is a static method" end end MyModel.do_something # => "this is a static method" MyModel::do_something # => "this is a static method"
来源:https://stackoverflow.com/questions/13067831/calling-ruby-method-without-instantiating-class