Extending Rails models
Rails models come with certain built-in methods like this:
Appointment.new
Appointment开发者_如何转开发.find(1)
How do I add more methods to Appointment
? It's apparently not done by adding methods to app/models/appointment.rb
. Doing that adds methods to an instance of Appointment
, but I want to add methods to Appointment
itself. How do I do that?
def self.some_method
#do stuff
end
Mark's answer is definitely right, but you will also see the following syntax when defining class methods:
class Appointment
class << self
def method1
# stuff
end
def method2
# stuff
end
def method3
# stuff
end
end
end
精彩评论