开发者

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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜