Jason Swett
Jason Swett

Reputation: 45174

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?

Upvotes: 4

Views: 820

Answers (2)

Peter Brown
Peter Brown

Reputation: 51717

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

Upvotes: 3

mark
mark

Reputation: 10574

def self.some_method
  #do stuff
end

Upvotes: 12

Related Questions