开发者

Extending Rails models

开发者 https://www.devze.com 2023-02-03 18:37 出处:网络
Rails models come with certain built-in methods like this: Appointment.new Appointment开发者_如何转开发.find(1)

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

精彩评论

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

关注公众号