简体   繁体   中英

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM